Follow-up: Putting back a missing column from a data.frame into a list of dta.frames

I’m following up on this question. My LIST of data.frames below is made from my data. However, this LIST is missing the paper column which is available in the original data.

I was wondering how to put the missing paper column back into LIST to achieve my DESIRED_LIST below?

I tried the solution suggested in this question (lapply(LITS,function(x)merge(x,data)[names(data)])) but it doesn’t produce my DESIRED_LIST.

Reproducible data and code are below.

paper     study sample    comp
1         1     1         1
1         2     2         2
1         2     3         3
2         3     4         4
2         3     4         4
2         3     4         5
2         3     4         5"
data <- read.table(text=m2,h=T)

        LIST <- list(data.frame(study=1       ,sample=1       ,comp=1),
                     data.frame(study=c(2,2)  ,sample=c(2,3)  ,comp=c(2,3)))

DESIRED_LIST <- list(data.frame(paper=1       ,study=1       ,sample=1       ,comp=1),
                     data.frame(paper=rep(1,2),study=c(2,2)  ,sample=c(2,3)  ,comp=c(2,3)))

>Solution :

One option is to loop over the list (‘LIST’), subset the data based with %in% on the pasteed rows of data and the list element data

LIST2 <-  lapply(LIST, function(x) 
    data[, data[names(x)]) %in%, x),])


> all.equal(DESIRED_LIST, LIST2, check.attributes = FALSE)
[1] TRUE

Leave a Reply