I have one dataframe (df1) with more than 200 columns containing data (several thousands of rows each). Column names are alphanumeric and all distinct from each other.
I have a second dataset (df2) with a couple of columns where the first column (named 'col1') contains rows with "values" carrying colnames of df1.
But not for every row in df2 I have a corresponding column in df1.
Now I would like to delete (drop) all rows in df2 where there is no "corresponding" column in df1.
I searched quite a while using keywords like "subset data.frame by values from another data.frame" but did not find any solution. I checked, e.g. here, here or here and some other places.
Thanks for your help.
dput()
to share reproducibly. – Gregor Thomasdf2[df2$col1 %in% names(df1), ]
. It doesn't seem to matter at all thatdf1
is a data frame, the only thing that matters is that you have a chracter vector of values you want to keep, and that happens to benames(df1)
. – Gregor Thomas