根据 R 中的条件和最小时间戳,按组第一行创建新变量
Create new variable based on first row by group, based on condition and minimum timestamp in R
我想创建一个新变量来告诉我特定行是否符合以下模式:event = A and timestamp = minimum by group,以确定每个参与者 ID 的行是否是事件 A 的第一行。
这是我正在使用的示例数据集:
participant_id <- c("ps1", "ps1", "ps1", "ps1", "ps2", "ps2", "ps3", "ps3", "ps3", "ps3")
timestamp <- c(0.01, 0.02, 0.03, 0.04, 0.01, 0.02, 0.01, 0.02, 0.03, 0.04)
event <- c("A", "A", "A", "B", "B", "A", "A", "A", "B", "A")
data.frame(participant_id, timestamp, event)
注意。数据不一定按升序出现。
这就是我想要结束的:
participant_id
timestamp
event
first_A_row
ps1
0.01
A
TRUE
ps1
0.02
A
FALSE
ps1
0.03
A
FALSE
ps1
0.04
B
FALSE
ps2
0.01
B
FALSE
ps2
0.02
A
TRUE
ps3
0.01
A
TRUE
ps3
0.02
A
FALSE
ps3
0.03
B
FALSE
ps3
0.04
A
FALSE
我们可能需要在按 'participant_id' 分组后为 'event' 'A' 子集 'timestamp' 并创建逻辑
library(dplyr)
df1 %>%
group_by(participant_id) %>%
mutate(first_A_row = timestamp == min(timestamp[event == 'A'])) %>%
ungroup
-输出
# A tibble: 10 × 4
participant_id timestamp event first_A_row
<chr> <dbl> <chr> <lgl>
1 ps1 0.01 A TRUE
2 ps1 0.02 A FALSE
3 ps1 0.03 A FALSE
4 ps1 0.04 B FALSE
5 ps2 0.01 B FALSE
6 ps2 0.02 A TRUE
7 ps3 0.01 A TRUE
8 ps3 0.02 A FALSE
9 ps3 0.03 B FALSE
10 ps3 0.04 A FALSE
数据
df1 <- data.frame(participant_id, timestamp, event)
我想创建一个新变量来告诉我特定行是否符合以下模式:event = A and timestamp = minimum by group,以确定每个参与者 ID 的行是否是事件 A 的第一行。
这是我正在使用的示例数据集:
participant_id <- c("ps1", "ps1", "ps1", "ps1", "ps2", "ps2", "ps3", "ps3", "ps3", "ps3")
timestamp <- c(0.01, 0.02, 0.03, 0.04, 0.01, 0.02, 0.01, 0.02, 0.03, 0.04)
event <- c("A", "A", "A", "B", "B", "A", "A", "A", "B", "A")
data.frame(participant_id, timestamp, event)
注意。数据不一定按升序出现。
这就是我想要结束的:
participant_id | timestamp | event | first_A_row |
---|---|---|---|
ps1 | 0.01 | A | TRUE |
ps1 | 0.02 | A | FALSE |
ps1 | 0.03 | A | FALSE |
ps1 | 0.04 | B | FALSE |
ps2 | 0.01 | B | FALSE |
ps2 | 0.02 | A | TRUE |
ps3 | 0.01 | A | TRUE |
ps3 | 0.02 | A | FALSE |
ps3 | 0.03 | B | FALSE |
ps3 | 0.04 | A | FALSE |
我们可能需要在按 'participant_id' 分组后为 'event' 'A' 子集 'timestamp' 并创建逻辑
library(dplyr)
df1 %>%
group_by(participant_id) %>%
mutate(first_A_row = timestamp == min(timestamp[event == 'A'])) %>%
ungroup
-输出
# A tibble: 10 × 4
participant_id timestamp event first_A_row
<chr> <dbl> <chr> <lgl>
1 ps1 0.01 A TRUE
2 ps1 0.02 A FALSE
3 ps1 0.03 A FALSE
4 ps1 0.04 B FALSE
5 ps2 0.01 B FALSE
6 ps2 0.02 A TRUE
7 ps3 0.01 A TRUE
8 ps3 0.02 A FALSE
9 ps3 0.03 B FALSE
10 ps3 0.04 A FALSE
数据
df1 <- data.frame(participant_id, timestamp, event)