0
votes

I have a dataframe DF1. id denotes participant's number, and then we have few observations (rows) for each participant:

id blocktype condition blocknr markodd
 1         1         1       1       0
 1         3         2       2       0
 1         3         3       2       0
 2         1         2       1       0
 2         1         1       2       0
 2         1         1       2       0
 3         4         1       1       0
 3         1         1       2       0
 3         2         1       2       0

I also have another data frame DF2, with additional data, this time with single line for each person:

 id taskorder exporder
 1         1        1
 2         2        1
 3         1        2

I would like to take a value from DF2 for each id, and copy and multiply it across all observations for the respective id, all in a new column of DF1, so that I get this:

id blocktype condition blocknr markodd taskorder
 1         1         1       1       0       1
 1         3         2       2       0       1
 1         3         3       2       0       1
 2         1         2       1       0       2
 2         1         1       2       0       2
 2         1         1       2       0       2
 3         4         1       1       0       1
 3         1         1       2       0       1
 3         2         1       2       0       1

Can you please tip me how to do it? dplyr solution would be most preferable!

1
merge and match will achieve what you needBENY
taskorder takes value 3 in your output and not in your inputMoody_Mudskipper

1 Answers

1
votes

Try this :

DF1 <- DF1 %>% left_join(DF2, by="id") %>% dplyr::select(colnames(DF1), taskorder)