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, eg here , here or here and some other places.
Thanks for your help.
Data:
df1 <- data.frame(a = 1:3, b = 1:3)
# a b
# 1 1 1
# 2 2 2
# 3 3 3
df2 <- data.frame(col1 = c("a", "c"))
# col1
# 1 a
# 2 c
Keep rows in df2
whose values are names in df1
:
subset(df2, col1 %in% names(df1))
# col1
# 1 a
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.