Home > database >  data wrangling using R
data wrangling using R

Time:12-15

I need your kind help typing data using R. A sample of my original data looks like this:

> dput(mydata1)
structure(list(subject = c("E1", "E1", "E1", "E1", "E1", "E1", 
"E1", "E1", "E1", "E1", "E1", "E1"), block = c(3, 3, 3, 4, 4, 
4, 5, 5, 5, 6, 6, 6), condition = c("EI", "EI", "I", "EI", "EI", 
"I", "EI", "EI", "I", "EI", "EI", "I"), RT = c(271, 370, 469, 
409, 560, 1506, 544, 544, 978, 452, 340, 645), Item_number = c(1, 
2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3)), row.names = c(NA, -12L), class = c("tbl_df", 
"tbl", "data.frame"))
 
     > mydata1
# A tibble: 12 x 5
   subject block condition    RT Item_number
   <chr>   <dbl> <chr>     <dbl>       <dbl>
 1 E1          3 EI          271           1
 2 E1          3 EI          370           2
 3 E1          3 I           469           3
 4 E1          4 EI          409           1
 5 E1          4 EI          560           2
 6 E1          4 I          1506           3
 7 E1          5 EI          544           1
 8 E1          5 EI          544           2
 9 E1          5 I           978           3
10 E1          6 EI          452           1
11 E1          6 EI          340           2
12 E1          6 I           645           3 

I am trying to compare RT value for condition EI in block 3 with RT values for condition I in block 3, 4, 5, and 6. So, for the purpose of the research question I am trying to address, I am considering all RT values for condition I while I am only considering the RT value for condition EI in block 3. To make the comparison easy to read by R, I would like to replace the RT values for condition EI in block 4, 5 and 6 by the RT value of the same condition in block 3.

So, if the condition is EI and the block is either 4,5,6: then, the RT value for this condition in that block for that Item_number and subject should be equivalent to the RT value for the same condition, Item_number, and subject in block 3. This should work for condition EI across each subject and Item_number.

The desired output should be something like this:

    > dput(data2_cleaned)
structure(list(subject = c("E1", "E1", "E1", "E1", "E1", "E1", 
"E1", "E1", "E1", "E1", "E1", "E1"), block = c(3, 3, 3, 4, 4, 
4, 5, 5, 5, 6, 6, 6), condition = c("EI", "EI", "I", "EI", "EI", 
"I", "EI", "EI", "I", "EI", "EI", "I"), RT = c(271, 370, 469, 
271, 370, 1506, 271, 370, 978, 271, 370, 645), Item_number = c(1, 
2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3)), row.names = c(NA, -12L), class = c("tbl_df", 
"tbl", "data.frame"))

    > data2_cleaned
# A tibble: 12 x 5
   subject block condition    RT Item_number
   <chr>   <dbl> <chr>     <dbl>       <dbl>
 1 E1          3 EI          271           1
 2 E1          3 EI          370           2
 3 E1          3 I           469           3
 4 E1          4 EI          271           1
 5 E1          4 EI          370           2
 6 E1          4 I          1506           3
 7 E1          5 EI          271           1
 8 E1          5 EI          370           2
 9 E1          5 I           978           3
10 E1          6 EI          271           1
11 E1          6 EI          370           2
12 E1          6 I           645           3

I would greatly appreciate your kind help and thoughts in how to achieve the desired output.

CodePudding user response:

You could try the following with dplyr. After grouping by subject and Item_number, check to see if the block is 4-6 and condition is "EI" - if so, change the RT value to the one where block is 3.

library(dplyr)

mydata1 %>%
  group_by(subject, Item_number) %>%
  mutate(RT = ifelse(block %in% 4:6 & condition == "EI", RT[block == 3], RT))

Output

   subject block condition    RT Item_number
   <chr>   <dbl> <chr>     <dbl>       <dbl>
 1 E1          3 EI          271           1
 2 E1          3 EI          370           2
 3 E1          3 I           469           3
 4 E1          4 EI          271           1
 5 E1          4 EI          370           2
 6 E1          4 I          1506           3
 7 E1          5 EI          271           1
 8 E1          5 EI          370           2
 9 E1          5 I           978           3
10 E1          6 EI          271           1
11 E1          6 EI          370           2
12 E1          6 I           645           3
  • Related