I have a couple of data frames, some of the row names in both are the same. I want to copy a column from first data frame for all the row names that are present in second data frame. First data frame (df1) looks like
m1 m2 m3
P001 60.00 2.0 1
P002 14.30 2.077 1
P003 29.60 2.077 1
P004 10.30 2.077 1
P006 79.30 2.077 1
P008 9.16 2.077 1
and the second data frame (df2) looks like
n1 n2 n3
P001 12.00 2.0 1
P003 17.60 1.7 1
P005 22.30 2.7 1
P006 26.30 1.7 1
I want to have variable m1 (df1$m1) for all row names present in second data frame( i.e P001, P003, P005, and P006). If some row names does not exist in df1 (eg P005), it could be replaced with NA or 0.
The answer could be something like this
m1
P001 60.00
P003 29.60
P005 NA
P006 79.30
The longer option would be to use loops but I am sure R should have a shortcut.