[R] Basic data frame manipulation

Jon BR jonsleepy at gmail.com
Mon Feb 23 22:25:09 CET 2015


Hi R-help,
    Although I know that variations of this question are frequently asked,
I searched and haven't found an answer for this specific variant, and
wonder if any of you know this off the top of your head:

df1 <- data.frame(a = 1:5,
                  row.names = letters[1:5]) # letters a to e
df2 <- data.frame(a = 1:5,
                  row.names = letters[3:7]) # letters c to g
df3 <- data.frame(a = 1:5,
                  row.names = letters[c(1,2,3,5,7)]) # letters a, b, c, e,
and g


I would like a command to produce a data frame which contains the same rows
(with rownames) as in df1, with elements in the columns corresponding to
the values present in each of the data frames (if there exists a matching
row; else NA if not present).  This should ideally work even if the rows
are in random order and if not sorted.

The result would look something like this:

df1.a df2.a df3.a
a 1 NA 1
b 2 NA 2
c 3 1 3
d 4 2 NA
e 5 3 4

Thank you in advance for any tips.

Jonathan

	[[alternative HTML version deleted]]



More information about the R-help mailing list