我正在跟進這個問題。LIST下面我的 data.frames 是由我的data. 但是,這LIST缺少paper原始data.
我想知道如何將丟失的paper列放回LIST以實作我的DESIRED_LIST下面?
我嘗試了這個問題( lapply(LITS,function(x)merge(x,data)[names(data)])) 中建議的解決方案,但它不會產生我的DESIRED_LIST.
可重現的資料和代碼如下。
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)))
uj5u.com熱心網友回復:
一種選擇是回圈比list(“LIST”),子集的data基于與%in%所述paste資料的編輯的行和串列元素資料
LIST2 <- lapply(LIST, function(x)
data[do.call(paste, data[names(x)]) %in% do.call(paste, x),])
-檢查
> all.equal(DESIRED_LIST, LIST2, check.attributes = FALSE)
[1] TRUE
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/351065.html
