Home > Back-end >  Remove row from dataframe with identique colname in other dataframe in R
Remove row from dataframe with identique colname in other dataframe in R

Time:10-25

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

CodePudding user response:

df2[df2$type %in% intersect(colnames(df1), df2$type), ]
  •  Tags:  
  • rrow
  • Related