0

I have two dataframes with the same number of columns but different number of rows.

For example

df1

    name1   name2   name3 
a     A      B        G
b     D      A        R
c     F      T        Q
d     Y      W        A
e     G      Y        D


df2

    name1   name2   name3 
a     A      B        C
b     D      A        I
c     A      T        G
e     G      Y        D
f     E      T        G
g     A      C        T

My dataframes are much bigger than this examples. Is there any simple way to remove rows from first dataframe which don't exist in second dataframe? I have to remove rows by rownames, so the output should be:

df1

    name1   name2   name3 
a     A      B        G
b     D      A        R
c     F      T        Q
e     G      Y        D

d row was removed because it isn't in the second dataframe.

Sotos
  • 51,121
  • 6
  • 32
  • 66
  • 2
    Using the set function, `intersect`, you could do `df1[intersect(rownames(df1), rownames(df2)),]`. – lmo Jun 13 '17 at 16:01
  • Possible duplicate of [How to join (merge) data frames (inner, outer, left, right)?](https://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right) – polka Jun 13 '17 at 16:55

1 Answers1

2

You can just compare the rownames and select those rows.

df1[which(rownames(df1) %in% rownames(df2)),]
  name1 name2 name3
a     A     B     G
b     D     A     R
c     F     T     Q
e     G     Y     D
G5W
  • 36,531
  • 10
  • 47
  • 80
  • Yes, this just computes what is wanted. If you want to change the data frame use `df1 = df1[which(rownames(df1) %in% rownames(df2)),] ` – G5W Jun 13 '17 at 16:11