I'm trying to match corresponding values of two columns in two different data frames. For every subc-year pat.id-wise pair (e.g. 14X-1991) in df1, I'd like to search df2 to create a list/vector/etc with all the df2$pat.id of matching combinations (for the example above, US18 and US20).
As a sample:
df1:
pat.id subc year
US1 14X 1991
US3 15R 1992
US5 10R 1990
df2:
pat.id subc year
US18 14X 1991
US20 14X 1991
US33 15R 1992
US34 15R 1992
US37 15R 1992
US50 10R 1990
Data:
df1 <- data.frame(cbind(c("US1", "US3", "US5"), c("14X", "15R", "10R"), c("1991", "1992", "1990")))
colnames(df1) <- c("pat.id", "subc", "year")
df2 <- data.frame(cbind(c("US18", "US20", "US33", "US34", "US37", "US50"), c("14X", "14X", "15R", "15R", "15R", "10R"), c("1991", "1991", "1992", "1992", "1992", "1990")))
colnames(df2) <- c("pat.id", "subc", "year")
Plugging in concrete values, it has worked for me with
df2$pat.id[which(df2$year==1991 & df2$subc=="14X")]. Now, I'd like to loop through all rows in df1.
Thank you!