merge - Merging more than 2 dataframes in R by rownames -


i gather data 4 df's , merge them rownames. looking efficient way this. simplified version of data have.

df1           <- data.frame(n= sample(seq(9, 27, 0.5), 40, replace= t),                             p= sample(seq(0.3, 4, 0.1), 40, replace= t),                             c= sample(seq(400, 500, 1), 40, replace= t)) df2           <- data.frame(origin= sample(c("a", "b", "c", "d", "e"), 40,                                            replace= t),                             foo1= sample(c(t, f), 40, replace= t),                             x= sample(seq(145600, 148300, 100), 40, replace= t),                             y= sample(seq(349800, 398600, 100), 40, replace= t)) df3           <- matrix(sample(seq(0, 1, 0.01), 40), 40, 100) df4           <- matrix(sample(seq(0, 1, 0.01), 40), 40, 100) rownames(df1) <- paste("p", sprintf("%02d", c(1:40)), sep= "") rownames(df2) <- rownames(df1) rownames(df3) <- rownames(df1) rownames(df4) <- rownames(df1) 

this do:

# merge df1 , df2 dat           <- merge(df1, df2, by= "row.names", all.x= f, all.y= f) #merge rownames(dat) <- dat$row.names #reset rownames dat$row.names <- null  #remove added rownames col  # merge dat , df3 dat           <- merge(dat, df3, by= "row.names", all.x= f, all.y= f) #merge rownames(dat) <- dat$row.names #reset rownames dat$row.names <- null  #remove added rownames col  # merge dat , df4 dat           <- merge(dat, df4, by= "row.names", all.x= f, all.y= f) #merge rownames(dat) <- dat$row.names #reset rownames dat$row.names <- null #remove added rownames col 

as can see, requires lot of code. question if same result can achieved more simple means. i've tried (without success): update: works now!

mymerge       <- function(x, y){   df            <- merge(x, y, by= "row.names", all.x= f, all.y= f)   rownames(df)  <- df$row.names   df$row.names  <- null   return(df) } dat           <- reduce(mymerge, list(df1, df2, df3, df4)) 

thanks in advance suggestions

join_all plyr want. must data frames , rownames added column

require(plyr)  df3 <- data.frame(df3) df4 <- data.frame(df4)  df1$rn <- rownames(df1) df2$rn <- rownames(df2) df3$rn <- rownames(df3) df4$rn <- rownames(df4)  df <- join_all(list(df1,df2,df3,df4), = 'rn', type = 'full') 

type argument should if rownames vary , not match if not want rownames:

df$rn <- null 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

java - Using an Integer ArrayList in Android -