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@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.

Reply via email to