0

I have data sets for four time points of the same subjects. Unfortunately, the subjects have different IDs in those data set. Thus, my data looks something like this:

df1 <- data.frame(ID = c("ALKJAD", "FAJOQF", "PQDJSLC"),
                  v1 = c(1,2,3),
                  V2 = c(4,5,6))


df2 <- data.frame(ID = c("KFOWQP", "QODPFL", "XBCMVB"),
                  v1 = c(2,4,6),
                  V2 = c(5,7,9))

df3 <- data.frame(ID = c("DWTHKU", NA, "SCBFHT"),
                  v1 = c(3, NA, 9),
                  V2 = c(4, NA, 8))

dfID <- data.frame(ID_t1 = c("ALKJAD", "FAJOQF", "PQDJSLC"),
                   ID_t2 = c("KFOWQP", "QODPFL", "XBCMVB"),
                   ID_t3 = c("DWTHKU", NA, "SCBFHT"))

How can I combine the four data sets based on this list?

Robn
  • 67
  • 8

1 Answers1

1

You could do two joins (although difficult to say if this code works without reproducible data).

library(dplyr)
inner_join(df1, df_ID, by = c("ID" = "ID_t1")) %>%
   inner_join(., df2, by = c("ID_t2" = "ID"))
Maël
  • 45,206
  • 3
  • 29
  • 67