Reputation: 29
Hi I am a beginner in R, I have a df named di_time as follow:
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
I would like to replace all trial x to trial (x+6) if first_block== FALSE and first_block== TRUE remain the same.
Upvotes: 0
Views: 56
Reputation: 102880
Here is a solution with base R
, where within()
and ifelse()
are used:
df <- within(df,trial <- ifelse(!first_block, trial+6, trial))
or
df <- within(df,trial <- trial + ifelse(!first_block, 6, 0))
such that
> 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
DATA
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"))
Upvotes: 0
Reputation: 51612
A base R approach can be,
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
Upvotes: 0
Reputation: 389325
You could do
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
This adds 6 when first_block == FALSE
and keeps the value as it is for TRUE
value.
data
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"))
Upvotes: 2