[R] Basic data frame manipulation

Ista Zahn istazahn at gmail.com
Mon Feb 23 22:33:56 CET 2015


Something like

tmp <- merge(df1, df2, by = "row.names", all.x = TRUE)
merge(tmp, df3, by.x = "Row.names", by.y = "row.names", all.x = TRUE)

perhaps?



On Mon, Feb 23, 2015 at 4:25 PM, Jon BR <jonsleepy at gmail.com> wrote:
> 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]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list