【发布时间】:2021-08-06 08:49:02
【问题描述】:
我正在跟进this 的回答。 that answer 的输出效果很好(见下文):
tlist.mpre tlist.sdpre tlist.n clist.mpre clist.sdpre clist.n
Dlsk_Krlr.102 81.6 10.8 73 80.5 11.2 80
Dlsk_Krlr.103 85.7 13.7 66 90.3 6.6 74
Dlsk_Krlr.104 81.4 10.9 72 80.5 11.2 80
Dlsk_Krlr.105 90.4 8.2 61 90.3 6.6 74
但是,我想从data 中提取两个名为nms = c('time_wk','treats') 的列并将其添加到上述输出中。但是当我添加 nms 时,新的输出(见下面 R 代码的结尾)会被打乱。
鉴于我在下面的可重现 R 代码是否有修复?
data <- read.csv("https://raw.githubusercontent.com/rnorouzian/m2/main/q.csv")
nms = c('time_wk','treats')
m = split(data, data$study.name)
(mm = m["Dlsk_Krlr"])
(input <- lapply(mm, function(i)
rev(expand.grid(post = unique(i$post),outcome = unique(i$outcome)))))
res <- setNames(lapply(1:0, function(i) lapply(input, function(inp) Map(function(o, p)
do.call(rbind, lapply(mm, function(x)
x[x$control == i & x$post == p & x$outcome == o, , drop = FALSE])),
inp$outcome, inp$post))), c("clist", "tlist"))
(aa = setNames(lapply(seq_along(res), function(i) Filter(NROW, res[[i]][[1]])), names(res)))
b <- lapply(aa, function(x) {
y <- do.call(rbind, x);
y[order(y$group), c("mpre", "sdpre", "n", nms)] }) ## I'm adding `nms` HERE but that scrambles the output below
cc = do.call(cbind,rev(b))
cc_1 = cc[!duplicated(cc),]
names(cc_1)[1:6] = c('mT','sdT','nT','mC','sdC','nC')
### NEW SCRAMBLED OUTPUT AFTER ADDING `nms`:
# mT sdT nT mC sdC nC clist.sdpre clist.n clist.time_wk clist.treats
#Dlsk_Krlr.102 81.6 10.8 73 1 2 80.5 11.2 80 1 2
#Dlsk_Krlr.103 85.7 13.7 66 1 2 90.3 6.6 74 1 2
#Dlsk_Krlr.104 81.4 10.9 72 1 2 80.5 11.2 80 1 2
#Dlsk_Krlr.105 90.4 8.2 61 1 2 90.3 6.6 74 1 2
【问题讨论】:
-
乱码是什么意思?看到和之前得到的结果差不多
-
@jared_mamrot 没有。
nms是一个向量 -
那是你重命名时犯的错误。不是来自代码。例如输入
cc并查看结果。 -
只删除另外两列
cc[grep("clist.(time_wk|treats)", names(cc), invert = TRUE)] -
是的,
cc[grep(sprintf("clist.(%s)", paste0(nms, collapse="|")), names(cc), invert = TRUE)]
标签: r list dataframe function loops