我想使用Trigger和Imageonset列创建一个新列ReactionTime。 对于以下突变,我只想使用> 0的触发器值。我该怎么做?
data <- data %>% mutate(ReactionTime = Trigger-Imageonset)
我的数据如下:
Subject Imageonset Trigger
1 25438.889 0.000
1 1251.783 0.000
1 1304.119 1308.677
1 1331.455 1334.053
1 1351.956 0.000
1 1374.707 1378.588
1 1400.092 0.000
1 1422.577 1426.804
1 1450.037 1453.035
...
我想得到这个结果:
Subject Imageonset Trigger ReactionTime
1 25438.889 0.000 0
1 1251.783 0.000 0
1 1304.119 1308.677 4.558
1 1331.455 1334.053 2.608
1 1351.956 0.000 0
1 1374.707 1378.588 3.881
1 1400.092 0.000 0
1 1422.577 1426.804 1.227
1 1450.037 1453.035 2.998
...
答案 0 :(得分:3)
一些想法:
# use ifelse
data <- data %>% mutate(ReactionTime = ifelse(Trigger > 0, Trigger-Imageonset, 0))
# use dplyr::if_else
data <- data %>% mutate(ReactionTime = if_else(Trigger > 0, Trigger-Imageonset, 0))
# use multiplication
data <- data %>% mutate(ReactionTime = Trigger-Imageonset * (Trigger > 0))
# use replace
data <- data %>% mutate(
ReactionTime = Trigger-Imageonset,
ReactionTime = replace(ReactionTime, Trigger == 0, 0)
)