我有一个数据帧'all',它的子集看起来像这个
ONSETS_S DURATIONS_S CUMUlATIVE_DURATION Phase Active Block TRIAL
0 2.926 2.926 1 NA NA NA
2.926 1.925 4.851 1 NA NA NA
4.851 4.851 9.702 1 Passive Baseline 1
9.702 1.54 11.242 1 Passive Baseline 1
11.242 4.851 16.093 1 Passive Baseline 1
107.183 1.925 109.109 1 NA NA NA
109.108 4.851 113.96 1 Active Baseline 1
114.135 1.848 115.808 1 Active Baseline 1
115.807 4.851 120.659 1 Active Baseline 1
312.232 1.925 314.16 1 NA NA NA
314.157 4.851 319.011 1 Passive Baseline 1
319.008 2.387 321.398 1 Passive Baseline 1
321.395 4.851 326.249 1 Passive Baseline 1
415.797 1.925 417.725 1 NA NA NA
417.722 4.851 422.576 1 Active GD 2
422.762 2.31 424.886 1 Active GD 2
424.883 4.851 429.737 1 Active GD 2
在整个数据帧中,总共有6个阶段,块中有5种类型的条目,每个阶段每个块最多有2次试验。当阶段、活动和块条件值固定时,我想为每个试验在ONSETS_S中创建一个最小值矩阵。例如,当Phase == 1 & Active == "Passive" & Block == "Baseline"
时,输出应该是P_1_Passive_Onsets <- [4.851 314.157]
使用dplyr
的简单解决方案。
目前还不完全清楚你想要什么,但从你的描述中,听起来你想在TRIAL、Phase、Active和Block级别之间进行分组,并找到每个级别的最小/最大值。
library(dplyr)
dat %>%
group_by(TRIAL, Phase, Active, Block) %>%
summarise(min_val = min(ONSETS_S), max_val = max(ONSETS_S))
# A tibble: 4 × 6
# Groups: TRIAL, Phase, Active [4]
TRIAL Phase Active Block min_val max_val
<int> <int> <chr> <chr> <dbl> <dbl>
1 1 1 Active Baseline 109. 116.
2 1 1 Passive Baseline 4.85 321.
3 2 1 Active GD 418. 425.
4 NA 1 NA NA 0 416.
或使用data.table
library(data.table)
setDT(dat)
dat[, list(min_val = min(ONSETS_S), max_val = max(ONSETS_S)), by=c("TRIAL", "Phase", "Active", "Block")]
TRIAL Phase Active Block min_val max_val
1: NA 1 <NA> <NA> 0.000 415.797
2: 1 1 Passive Baseline 4.851 321.395
3: 1 1 Active Baseline 109.108 115.807
4: 2 1 Active GD 417.722 424.883