Remi Genevest
2015-Jan-17 16:37 UTC
[R] extra arguments in do.call applied to a list of dataframes
Hello, I have a list of dataframes with different number of rows and I want to bind columns by rownames and put some NAs where rownames do not match. I was thinking about doing something like this : do.call(merge,c(mylist,by="row.names",all.x=TRUE)) but I get the following error message : /Erreur dans fix.by(by.x, x) : 'by' doit sp?cifier lune ou plusieurs colonnes comme des entiers, des noms ou des valeurs logiques / Have you any ideas to fix it? Thing is that it works fine when I try with a list of 2 dataframes, but goes wrong with 3 and more dataframes. (see examples below) ## works well with 2 dataframes : mylist<-list() mylist[[1]]<-data.frame(y=seq(1,10)) mylist[[2]]<-data.frame(y=seq(1,5)) do.call(merge,c(mylist,by="row.names",all.x=TRUE)) Row.names y.x y.y 1 1 1 1 2 10 10 NA 3 2 2 2 4 3 3 3 5 4 4 4 6 5 5 5 7 6 6 NA 8 7 7 NA 9 8 8 NA 10 9 9 NA ## fails with 3 df : mylist<-list() mylist[[1]]<-data.frame(y=seq(1,10)) mylist[[2]]<-data.frame(y=seq(1,5)) mylist[[3]]<-data.frame(y=seq(1,7)) do.call(merge,c(mylist,by="row.names",all.x=TRUE)) /Erreur dans fix.by(by.x, x) : 'by' doit sp?cifier lune ou plusieurs colonnes comme des entiers, des noms ou des valeurs logiques/ Thanks for help !! -- View this message in context: http://r.789695.n4.nabble.com/extra-arguments-in-do-call-applied-to-a-list-of-dataframes-tp4701915.html Sent from the R help mailing list archive at Nabble.com.
Ista Zahn
2015-Jan-17 17:25 UTC
[R] extra arguments in do.call applied to a list of dataframes
I would just move the row.names to a column, and use Reduce instead of do.call. Like this: mylist <- lapply(mylist, function(x) data.frame(row = rownames(x), x)) Reduce(function(x, y){merge(x, y, by = "row", all=TRUE)}, mylist) Best, Ista On Sat, Jan 17, 2015 at 11:37 AM, Remi Genevest <rgenevest at free.fr> wrote:> Hello, > > I have a list of dataframes with different number of rows and I want to bind > columns by rownames and put some NAs where rownames do not match. > > I was thinking about doing something like this : > > do.call(merge,c(mylist,by="row.names",all.x=TRUE)) > > but I get the following error message : > > /Erreur dans fix.by(by.x, x) : > 'by' doit sp?cifier lune ou plusieurs colonnes comme des entiers, des noms > ou des valeurs logiques > / > > Have you any ideas to fix it? > > Thing is that it works fine when I try with a list of 2 dataframes, but goes > wrong with 3 and more dataframes. (see examples below) > > ## works well with 2 dataframes : > mylist<-list() > mylist[[1]]<-data.frame(y=seq(1,10)) > mylist[[2]]<-data.frame(y=seq(1,5)) > do.call(merge,c(mylist,by="row.names",all.x=TRUE)) > > Row.names y.x y.y > 1 1 1 1 > 2 10 10 NA > 3 2 2 2 > 4 3 3 3 > 5 4 4 4 > 6 5 5 5 > 7 6 6 NA > 8 7 7 NA > 9 8 8 NA > 10 9 9 NA > > ## fails with 3 df : > mylist<-list() > mylist[[1]]<-data.frame(y=seq(1,10)) > mylist[[2]]<-data.frame(y=seq(1,5)) > mylist[[3]]<-data.frame(y=seq(1,7)) > do.call(merge,c(mylist,by="row.names",all.x=TRUE)) > > /Erreur dans fix.by(by.x, x) : > 'by' doit sp?cifier lune ou plusieurs colonnes comme des entiers, des noms > ou des valeurs logiques/ > > Thanks for help !! > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/extra-arguments-in-do-call-applied-to-a-list-of-dataframes-tp4701915.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.