添加列向上计数,直到另一列中的值发生变化
Add column counting upwards until value in another column changes
我有一个名为 data_clean 的 df:
> head(data_clean)
participant type target_onset key_resp.rt central_size
1 1010 gap 0.4 0.3260000 0.15
2 1010 gap 0.1 0.3380001 0.15
3 1010 overlap 0 0.4480000 0.10
4 1010 gap 0.2 0.3940001 0.10
5 1010 gap 0.1 0.3980000 0.10
6 1010 gap 0.2 0.4990001 0.10
我想添加一个名为 trial_num 的列,每行 +=1,直到参与者列号发生变化。然后它应该重新开始。因此,如果下一位参与者在 6 次试验后开始,它将看起来像这样:
participant type target_onset key_resp.rt central_size trial_num
1 1010 gap 0.4 0.3260000 0.15 1
2 1010 gap 0.1 0.3380001 0.15 2
3 1010 overlap 0 0.4480000 0.10 3
4 1010 gap 0.2 0.3940001 0.10 4
5 1010 gap 0.1 0.3980000 0.10 5
6 1010 gap 0.2 0.4990001 0.10 6
7 1011 gap 0.4 0.3260000 0.15 1
8 1011 gap 0.1 0.3380001 0.15 2
9 1011 overlap 0 0.4480000 0.10 3
非常感谢任何建议。谢谢
在使用 group_by()
按组对您的 ID 应用任务后,使用 row_number()
尝试此操作。这里的代码使用 dplyr
:
library(dplyr)
#Code
new <- data_clean%>%
group_by(participant) %>%
mutate(trial_num=row_number())
输出:
# A tibble: 9 x 6
# Groups: participant [2]
participant type target_onset key_resp.rt central_size trial_num
<int> <chr> <dbl> <dbl> <dbl> <int>
1 1010 gap 0.4 0.326 0.15 1
2 1010 gap 0.1 0.338 0.15 2
3 1010 overlap 0 0.448 0.1 3
4 1010 gap 0.2 0.394 0.1 4
5 1010 gap 0.1 0.398 0.1 5
6 1010 gap 0.2 0.499 0.1 6
7 1011 gap 0.4 0.326 0.15 1
8 1011 gap 0.1 0.338 0.15 2
9 1011 overlap 0 0.448 0.1 3
使用了一些数据:
#Data
data_clean <- structure(list(participant = c(1010L, 1010L, 1010L, 1010L, 1010L,
1010L, 1011L, 1011L, 1011L), type = c("gap", "gap", "overlap",
"gap", "gap", "gap", "gap", "gap", "overlap"), target_onset = c(0.4,
0.1, 0, 0.2, 0.1, 0.2, 0.4, 0.1, 0), key_resp.rt = c(0.326, 0.3380001,
0.448, 0.3940001, 0.398, 0.4990001, 0.326, 0.3380001, 0.448),
central_size = c(0.15, 0.15, 0.1, 0.1, 0.1, 0.1, 0.15, 0.15,
0.1)), row.names = c("1", "2", "3", "4", "5", "6", "7", "8",
"9"), class = "data.frame")
我有一个名为 data_clean 的 df:
> head(data_clean)
participant type target_onset key_resp.rt central_size
1 1010 gap 0.4 0.3260000 0.15
2 1010 gap 0.1 0.3380001 0.15
3 1010 overlap 0 0.4480000 0.10
4 1010 gap 0.2 0.3940001 0.10
5 1010 gap 0.1 0.3980000 0.10
6 1010 gap 0.2 0.4990001 0.10
我想添加一个名为 trial_num 的列,每行 +=1,直到参与者列号发生变化。然后它应该重新开始。因此,如果下一位参与者在 6 次试验后开始,它将看起来像这样:
participant type target_onset key_resp.rt central_size trial_num
1 1010 gap 0.4 0.3260000 0.15 1
2 1010 gap 0.1 0.3380001 0.15 2
3 1010 overlap 0 0.4480000 0.10 3
4 1010 gap 0.2 0.3940001 0.10 4
5 1010 gap 0.1 0.3980000 0.10 5
6 1010 gap 0.2 0.4990001 0.10 6
7 1011 gap 0.4 0.3260000 0.15 1
8 1011 gap 0.1 0.3380001 0.15 2
9 1011 overlap 0 0.4480000 0.10 3
非常感谢任何建议。谢谢
在使用 group_by()
按组对您的 ID 应用任务后,使用 row_number()
尝试此操作。这里的代码使用 dplyr
:
library(dplyr)
#Code
new <- data_clean%>%
group_by(participant) %>%
mutate(trial_num=row_number())
输出:
# A tibble: 9 x 6
# Groups: participant [2]
participant type target_onset key_resp.rt central_size trial_num
<int> <chr> <dbl> <dbl> <dbl> <int>
1 1010 gap 0.4 0.326 0.15 1
2 1010 gap 0.1 0.338 0.15 2
3 1010 overlap 0 0.448 0.1 3
4 1010 gap 0.2 0.394 0.1 4
5 1010 gap 0.1 0.398 0.1 5
6 1010 gap 0.2 0.499 0.1 6
7 1011 gap 0.4 0.326 0.15 1
8 1011 gap 0.1 0.338 0.15 2
9 1011 overlap 0 0.448 0.1 3
使用了一些数据:
#Data
data_clean <- structure(list(participant = c(1010L, 1010L, 1010L, 1010L, 1010L,
1010L, 1011L, 1011L, 1011L), type = c("gap", "gap", "overlap",
"gap", "gap", "gap", "gap", "gap", "overlap"), target_onset = c(0.4,
0.1, 0, 0.2, 0.1, 0.2, 0.4, 0.1, 0), key_resp.rt = c(0.326, 0.3380001,
0.448, 0.3940001, 0.398, 0.4990001, 0.326, 0.3380001, 0.448),
central_size = c(0.15, 0.15, 0.1, 0.1, 0.1, 0.1, 0.15, 0.15,
0.1)), row.names = c("1", "2", "3", "4", "5", "6", "7", "8",
"9"), class = "data.frame")