Home > Back-end >  How can I filter into a new data frame conditional on the columns existing in another data frame?
How can I filter into a new data frame conditional on the columns existing in another data frame?

Time:11-16

So I have two dataframes.

df1:

   Date Bond2 Bond3 Bond5 Bond6 Bond7 Bond8 Bond10 Bond11 Bond12 Bond14 Bond15 Bond16 Bond17
1 41275    NA    NA    NA    NA    NA    NA     NA     NA     NA     NA     NA     NA     NA
2 41276    NA    NA    NA    NA    NA    NA     NA     NA     NA     NA     NA     NA     NA
3 41277    NA    NA    NA    NA    NA    NA     NA     NA     NA     NA     NA     NA     NA
4 41278    NA    NA    NA    NA    NA    NA     NA     NA     NA     NA     NA     NA     NA
5 41279    NA    NA    NA    NA    NA    NA     NA     NA     NA     NA     NA     NA     NA

df1:

   Date Bond2 Bond3 Bond4 Bond5 Bond6 Bond7 Bond8 Bond10 Bond11 Bond12 Bond14 Bond16 Bond17 Bond19
1 41275    NA    NA    NA    NA    NA    NA    NA     NA     NA     NA     NA     NA     NA     NA
2 41276    NA    NA    NA    NA    NA    NA    NA     NA     NA     NA     NA     NA     NA     NA
3 41277    NA    NA    NA    NA    NA    NA    NA     NA     NA     NA     NA     NA     NA     NA
4 41278    NA    NA    NA    NA    NA    NA    NA     NA     NA     NA     NA     NA     NA     NA

I want to create a new df3 which is df1 where all the columns are contained in df2, and then a df4 which is df2 where all the columns are contained within df1.

I was thinking along the lines of filter(df1, names() %in% names(df2)) or select(names(df1) %in% names(df2) but neither works.

Thanks

CodePudding user response:

If you want to stick to tidyr, one way would be:

df3 <- df1 %>% select(all_of(names(df2))
df4 <- df2 %>% select(all_of(names(df1))

CodePudding user response:

We may use intersect on the column names from 'df1', 'df2' and use that to subset the columns from df1, df2 to create df3, df4 respectively

nm1 <- intersect(names(df1), names(df2))
df3 <- df1[nm1]
df4 <- df2[nm1]
  • Related