0

I am trying to add a column from one dataframe to another. The data is long repeated measures data, with each ID having two rows. Both my main dataset (d) and my secondary dataset (d2) use the same column (ID) to link cases to participants. However, when I use mutate like this d <- mutate(d, x = d2$x) the column binds to the dataframe but the values are not tied to the ID. This means that data gets mixed up between participants.

Is there a way to make sure that the values are referenced by ID when I add the column?

C_bath
  • 53
  • 3
  • 2
    use `left_join(d, d2, by = "ID")` –  May 19 '22 at 12:30
  • left_join(d, d2[,c("ID","x"], by = "ID") – Lucca Nielsen May 19 '22 at 12:36
  • Please share your data using `dput(df)`? So we can reproduce your problem. – Quinten May 19 '22 at 12:46
  • 1
    It's easier to help you if you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick May 19 '22 at 12:51

0 Answers0