[R] Merge dataframes

jdanielnd jdanielnd at gmail.com
Sat Oct 8 17:48:31 CEST 2011


Hi everybody,

I got a solution for my problem with Eric Paniagua (many thanks!)! Thank you
a lot everybody!

Paniagua said explained that I was losing row.names when I merged a
data.frame with a factor object (data2$color), because a factor objects
don't have row.names. Instead of merging data1 to data2$color, I merged it
to data2["color"] (still a data.frame objcet). It preserves row.names.

When I do:

d1 <- merge(data1, data2["color"], by=0, all.x=T)

It creates a new variable on column 1, with the original row.names, so I
have to transform this data.frame in a new one, using this variable as the
row.names:

d2 <- data.frame(d1, row.names=1)

It does the job, but row.names are not in the same order as in data1, so I
reorder it:

d2[row.names(data1),]

And it gives me the dataframe data1, binded to the new variable color, with
the NAs in the proper position.

Thank you all!

Cheers,

Joao D.

--
View this message in context: http://r.789695.n4.nabble.com/Merge-dataframes-tp3882222p3885316.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list