Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In R, how can I filter into a new data frame conditional on the columns existing in another data frame?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

   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

>Solution :

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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading