嗨,我是R语言的初学者, 我有一个名为 di_time 的df,如下所示:
A tibble: 3,037 x 4
Groups: id, trial [1,519]
id trial first_block time
<int> <int> <lgl> <dbl>
1 866093 1 FALSE 145.
2 866093 1 TRUE 114.
3 866093 2 FALSE 125.
4 866093 2 TRUE 60.4
5 866093 3 FALSE 107.
6 866093 3 TRUE 19.1
7 866093 4 FALSE 86.9
8 866093 4 TRUE 10.2
9 866093 5 FALSE 66.2
10 866093 5 TRUE 33.9
with 3,027 more rows
如果first_block == FALSE和first_block == TRUE保持不变,我想将所有试验x替换为试验(x + 6)。
答案 0 :(得分:2)
你可以做
df$trial1 <- df$trial + ((!df$first_block) * 6)
df
# id trial first_block time trial1
#1 866093 1 FALSE 145.0 7
#2 866093 1 TRUE 114.0 1
#3 866093 2 FALSE 125.0 8
#4 866093 2 TRUE 60.4 2
#5 866093 3 FALSE 107.0 9
#6 866093 3 TRUE 19.1 3
#7 866093 4 FALSE 86.9 10
#8 866093 4 TRUE 10.2 4
#9 866093 5 FALSE 66.2 11
#10 866093 5 TRUE 33.9 5
这在first_block == FALSE
时加6,并保持TRUE
值的原样。
数据
df <- structure(list(id = c(866093L, 866093L, 866093L, 866093L, 866093L,
866093L, 866093L, 866093L, 866093L, 866093L), trial = c(1L, 1L,
2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L), first_block = c(FALSE, TRUE,
FALSE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, TRUE), time = c(145,
114, 125, 60.4, 107, 19.1, 86.9, 10.2, 66.2, 33.9)), class =
"data.frame", row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"))
答案 1 :(得分:0)
基本的R方法可以,
with(df, ave(trial, id, FUN = function(i)replace(i, first_block[1] == FALSE, i+6)))
#[1] 7 7 8 8 9 9 10 10 11 11
答案 2 :(得分:0)
这里是base R
的解决方案,其中使用了within()
和ifelse()
:
df <- within(df,trial <- ifelse(!first_block, trial+6, trial))
或
df <- within(df,trial <- trial + ifelse(!first_block, 6, 0))
如此
> df
id trial first_block time
1 866093 7 FALSE 145.0
2 866093 1 TRUE 114.0
3 866093 8 FALSE 125.0
4 866093 2 TRUE 60.4
5 866093 9 FALSE 107.0
6 866093 3 TRUE 19.1
7 866093 10 FALSE 86.9
8 866093 4 TRUE 10.2
9 866093 11 FALSE 66.2
10 866093 5 TRUE 33.9
数据
df <- structure(list(id = c(866093L, 866093L, 866093L, 866093L, 866093L,
866093L, 866093L, 866093L, 866093L, 866093L), trial = c(1L, 1L,
2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L), first_block = c(FALSE, TRUE,
FALSE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, TRUE), time = c(145,
114, 125, 60.4, 107, 19.1, 86.9, 10.2, 66.2, 33.9)), row.names = c(NA,
-10L), class = c("data.frame"))