0

I've created a dataframe as follows

test <- data.frame(ID = c(rep(1:60, each =11)), time = (c(rep(0:10))), resp = NA, liver = NA)

I would like to merge it with other dataframes, such as this one named resp_test, for every corresponding ID and time in the test dataframe.

ID  time   resp  
1       0 51
1       1 22
1       2 74
1       3 32
1       4 23
1       5 21
1       6 12
1       7 84
1       8 21
1       9 92
1       10 78

However, not all IDs in resp_test have the complete (0 -> 10) time data on the resp variable, like this one. Please note that the data for variable time = 4 is missing, as well as from time= 7 to time = 10.

    ID          time resp
1   13072516    2   NA
2   12964437    3   7.2
3   12964437    3   0.8
4   12964437    5   14.4
5   12964437    5   1.6
6   12964437    6   20.7
7   12964437    6   2.3
8   12964437    7   45.0
9   12964437    7   5.0
10  12964437    7   22.5
11  12964437    7   2.5
12  13115001    0   28.8
13  13115001    0   22.3

I have tried merge(test, resp_test, by = "time") but all I got was a empty data frame.

Any tips on solving this would be greatly appreciated.

dairelix
  • 77
  • 5

2 Answers2

2

Try

library(dplyr)
left_join(test, resp_test, by = "time")
writer_typer
  • 708
  • 7
  • 25
  • It worked wonderfully. I haven't thought of using ```left_join```, nor did I know you could use ```by = c("ID", "time")```. Thank you very much. – dairelix Sep 14 '20 at 17:20
2

To merge by both ID and time, you should do:

library(dplyr)

test %>% 
  select(-resp) %>% 
  left_join(resp_test, by = c("ID", "time"))
Count Orlok
  • 997
  • 4
  • 13
  • It worked wonderfully. I haven't thought of using ```left_join```, nor did I know you could use ```by = c("ID", "time")```. Thank you very much. – dairelix Sep 14 '20 at 17:20
  • May I ask, what's the difference between using merge() and left_join()? – dairelix Sep 14 '20 at 17:20