[R] Trying to write Merge for more data.frames - Error in match.names(clabs, names(xi))

Gabor Grothendieck ggrothendieck at gmail.com
Mon Jan 14 01:47:15 CET 2008


Note that the zoo package has a multiway merge for zoo class time
series:

> library(zoo)
as.matrix(do.call(merge, lapply(test, function(x) zoo(unname(x), names(x)))))
  A   B   C   D   X
A "a" "a" NA  NA  "a"
B "b" "b" NA  "b" "b"
C "c" "c" NA  "c" "c"
D "d" "d" NA  "d" "d"
E "e" "e" "e" "e" "e"
F "f" NA  "f" "f" "f"
G "g" NA  "g" NA  "g"
H "h" NA  "h" NA  "h"
I "i" NA  "i" NA  "i"
J "j" NA  "j" NA  "j"
K NA  NA  NA  NA  "k"
L NA  NA  NA  NA  "l"
M NA  NA  NA  NA  "m"


On Jan 13, 2008 6:47 PM, Jiří Voller <jirivoller at gmail.com> wrote:
> Dear list members,
> I would like to merge multiple dataframes and seems that this task is going
> to be required quite often, so I decided to write a simple (pseudo)recursive
> merge.
> I started with the case when dataframes are merged by rows (0). But there is
> a problem when a dataframe to be merged in the step n has some items that
> are not in previous ones. Then I get "Error in match.names(clabs, names(xi))
> :   names do not match previous names".
> Please help. Thank you Jiri Voller
>
> MergeByRownames<-function(x,a,all.x=T,all.y=T){
>    stopifnot(is.list(x))
>
>    x<-lapply(x,as.data.frame,row.names = NULL)
>     ## if (any(unlist(lapply(x,
> function(x){is.null(rownames(x))})))){stop("Missing
> names.")}
>           # doesnt work x<-lapply(x,as.data.frame,row.names = NULL)
> assignes names (1:n)
>
>    for (i in 1:length(x)){
>        if(i==1){
>            merged<-x[[i]]
>        }else{
>            if(i==2){
>                 merged<-merge(merged,x[[i]],by.x=0,by.y=0,all.x=all.x,
> all.y=all.y,sort=F)
>            } else {
>                 merged<-merge(merged,x[[i]],by.x="Row.names",by.y=0,
> all.x=all.x,all.y=all.y,sort=F)
>              }
>         }
>    }
> return(merged)
> }
>
> MergeByRownames(test) gives "Error in match.names(clabs, names(xi)) :
> names do not match previous names".
> Interestingly MergeByRownames.rec(list(test$A,test$A,test$A,test$X)) gives
> the same error, while MergeByRownames.rec(list(test$A,test$A,test$X)) works
> fine. I cannot see anything fishy on the data frame that is used to merge
> with final test$X.
>
> test<-structure(list(A = structure(c("a", "b", "c", "d", "e", "f",
> "g", "h", "i", "j"), .Names = c("A", "B", "C", "D", "E", "F",
> "G", "H", "I", "J")), B = structure(c("a", "b", "c", "d", "e"
> ), .Names = c("A", "B", "C", "D", "E")), C = structure(c("e",
> "f", "g", "h", "i", "j"), .Names = c("E", "F", "G", "H", "I",
> "J")), D = structure(c("b", "c", "d", "e", "f"), .Names = c("B",
> "C", "D", "E", "F")), X = structure(c("a", "b", "c", "d", "e",
> "f", "g", "h", "i", "j", "k", "l", "m"), .Names = c("A", "B",
> "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M"))), .Names = c("A",
> "B", "C", "D", "X"))
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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