0

What I want to do is match two dataframes by rownames. Not all the rownames are the same, when they not match R should give a NA in the n column. When the rownames match I want to calculate a ratio (b / a) in the n column. Does anyone have any tips on how to do this?

Here a example:

a <-        c
     name1  1
     name2  2
     name3  3
     name4  5

b <-        d
     name1  1
     name2  6
     name4  10


Final result

ab <-       c  d  n
     name1  1  1  1
     name2  2  6  3
     name3  3  NA NA
     name4  5  10 2
 



  

1 Answers1

1

We can use merge to join tables. Without using any packages:

ab <- merge(a, b, by = "row.names", all = TRUE)
ab$n <- ab$d/ab$c
koolmees
  • 2,725
  • 9
  • 23