Timo V
Timo V

Reputation: 141

Remove row from dataframe with identique colname in other dataframe in R

I have df1:

Name A B D
1
2
3
4

I have df2

Type Cow Cat Dog
A
B
C
D

I want to remove the row from df2 of which i have no column in df1. In this case i have no column C in df1 so i want to remove row C from df2.

Expected output

Type Cow Cat Dog
A
B
D

Upvotes: 0

Views: 29

Answers (1)

Maël
Maël

Reputation: 52004

df2[df2$type %in% intersect(colnames(df1), df2$type), ]

Upvotes: 1

Related Questions