Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)))

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

-checking

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading