【问题标题】:Extracting a data.frame from two lists in BASE R从 BASE R 中的两个列表中提取 data.frame
【发布时间】:2021-08-03 09:08:33
【问题描述】:

在下面的 R 代码中,我试图获取我的 desired output,其中 mpre1,sdpre1,n1 是从我的 aa 列表中提取的 control==FALSEmpre2,@当control==TRUE 时,987654328@,n2 是从我的aa 列表中提取的。另外两个提取索引是outcomepost,它们在mpre1... 端和mpre2... 端各不相同。

想法是在所需输出的每一行中,来自aa(见下文)的mpre1sdpre1n1元素集来自control==FALSE侧(aa)而来自aampre2,sdpre2,n2 元素集(见下文)来自control==TRUE 侧。

我想要的输出是否可以在 BASE R 中获得?

# Desired output (4 rows x 6 columns):
#  mpre1 sdpre1 n1 mpre2 sdpre2 n2
#1  81.6   10.8 73 80.50 11.20  80 
#2  85.7   13.7 66 90.30  6.60  74 
#3  81.4   10.9 72 80.50 11.20  80
#4  90.4    8.2 61 90.30  6.60  74

我试图形成一个expand.grid(),所以outcomepost的所有组合都被找到并提取,然后在其中提取control==FALSEtlist:对于mpre1,@987654354 @,n1),以及那些control==TRUE(clist: for mpre2,sdpre2,n2)。

data <- read.csv("https://raw.githubusercontent.com/rnorouzian/m2/main/q.csv")

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)))
#== CURRENT OUPUT:
#$clist
#$clist[[1]]
#          study.name group  n mpre sdpre mpos sdpos post control outcome time_wk treats
#Dlsk_Krlr  Dlsk_Krlr     3 80 80.5  11.2 80.8  10.7    1    TRUE       1       1      2

#$clist[[2]]
#          study.name group  n mpre sdpre mpos sdpos post control outcome time_wk treats
#Dlsk_Krlr  Dlsk_Krlr     3 74 90.3   6.6 89.6   6.3    1    TRUE       2       1      2


#$tlist
#$tlist[[1]]
#              study.name group  n mpre sdpre mpos sdpos post control outcome time_wk treats
#Dlsk_Krlr.102  Dlsk_Krlr     1 73 81.6  10.8 83.1  11.1    1   FALSE       1       1      2
#Dlsk_Krlr.104  Dlsk_Krlr     2 72 81.4  10.9 85.0   8.1    1   FALSE       1       1      2

#$tlist[[2]]
#              study.name group  n mpre sdpre mpos sdpos post control outcome time_wk treats
#Dlsk_Krlr.103  Dlsk_Krlr     1 66 85.7  13.7 88.8  10.5    1   FALSE       2       1      2
#Dlsk_Krlr.105  Dlsk_Krlr     2 61 90.4   8.2 91.2   7.6    1   FALSE       2       1      2

【问题讨论】:

    标签: r list dataframe function loops


    【解决方案1】:
    b <- lapply(aa, function(x)  {
               y<-do.call(rbind,  x)
              y[order(y$group), c("mpre", "sdpre", "n")] })
    do.call(cbind.data.frame,rev(b))
    
                 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
    

    【讨论】:

    • Onyambu,我对您的上述答案进行了有趣的跟进。想象一下,"mpre", "sdpre", "n" 都是 NA,使用你的解决方案我可以得到 4 行 6 列但所有 NA 值的最终输出吗?
    猜你喜欢
    • 2021-08-06
    • 1970-01-01
    • 1970-01-01
    • 2020-08-29
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    • 2019-11-10
    • 1970-01-01
    相关资源
    最近更新 更多