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

From: Ji Voller <jirivoller_at_gmail.com>
Date: Mon, 14 Jan 2008 00:47:37 +0100


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. Received on Sun 13 Jan 2008 - 23:52:08 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 14 Jan 2008 - 01:30:07 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive