【问题标题】:Follow-up: Extracting a data.frame from two lists in BASE R后续:从 BASE R 中的两个列表中提取一个 data.frame
【发布时间】: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


【解决方案1】:

这是基本的 R 解决方案:

cc[,grep(sprintf("clist.(%s)", paste0(nms, collapse="|")), names(cc), invert = TRUE)]

编辑:

确保 nms 排在最后:

d<-cc[grep(sprintf("clist.(%s)", paste0(nms, collapse="|")), names(cc), invert = TRUE)]

i1 <- grepl(sprintf("(%s)", paste0(nms, collapse="|")), names(d))

cbind(d[!i1], d[i1])

【讨论】:

  • 有可能。 @rnorouzian,你希望它在基础 R 中吗?
  • 例如将上面的结果保存为 d,然后再次使用 grep 将行移到末尾。
  • @rnorouzian 已编辑
猜你喜欢
  • 2021-08-03
  • 2016-08-27
  • 1970-01-01
  • 1970-01-01
  • 2021-09-03
  • 2023-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多