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

Time:11-07

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.

m2="
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=rep(3,4),sample=rep(4,4),comp=c(4,4,5,5)),
                     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(2,4),study=rep(3,4),sample=rep(4,4),comp=c(4,4,5,5)),
                     data.frame(paper=rep(1,2),study=c(2,2)  ,sample=c(2,3)  ,comp=c(2,3)))

CodePudding user response:

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[do.call(paste, data[names(x)]) %in% do.call(paste, x),])

-checking

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