Search code examples
rmergecbind

Merge dataframe and cbind list


I extracted from a data set the addition, maximum, and average value of a variable y jointly with the maximum value of x.

I also get the x value at maximum y value.

I pooled these values using cbind function. I want to merge this list with a dataframe, but got

error:Error in sort.list(bx[m$xi]) : 'x' must be atomic for 'sort.list'
Have you called 'sort' on a list?

Also, I tried the unlist function but didn´t gives me satistafctory data.

Here is the code I am using:

AUC<-lapply(split(data_example, data_example$class), function(d) sum(d$y))
max.y<-lapply(split(data_example, data_example$class), function(d) max(d$y))
max.x<-lapply(split(data_example, data_example$class), function(d) max(d$x))
auc.mean<-lapply(split(data_example, data_example$class), function(d) mean(d$y))
x.ymax<-lapply(split(data_example, data_example$class), function(d) 
d$x[which.max(d$y)])

data1<-cbind(AUC,max.y,max.x, auc.mean, x.ymax)

datafinal<-merge(data1, data_merge, by="class")

Here I uploaded the data for reproduce the example:

http://www.filedropper.com/dataexample_1 http://www.filedropper.com/datamerge


Solution

  • The reason cbind didn't work is because the objects were lists. There are couple of ways to correct this

    1) Replace lapply with sapply to get a vector output

    AUC <- sapply(split(data_example$y, data_example$class), sum)
    

    and similarly for the other cases and then cbind as in the OP's. Infact, it can be done on a single lapply/sapply too

    2) As the objects are lists, we unlist it and then cbind

    data1 <- data.frame(AUC = unlist(AUC),max.y = unlist(max.y), 
       max.x = unlist(max.x), auc.mean = unlist(auc.mean), 
       x.ymax = unlist(x.ymax), class = names(AUC))