从BASE R中的两个列表中提取一个data.frame [英] Extracting a data.frame from two lists in BASE R

查看:38
本文介绍了从BASE R中的两个列表中提取一个data.frame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的R代码中,我试图获取所需的输出,其中 mpre1 sdpre1 n1 是在 control == FALSE mpre2 sdpre2 时从我的 aa 列表中提取的当 control == TRUE 时,将从我的 aa 列表中提取n2 .其他两个提取索引是成果 post ,它们在 mpre1 ...方面和 mpre2 ...方面有所不同.一侧.

In my R code below, I'm trying to obtain my desired output in which mpre1,sdpre1,n1 are extracted from my aa list when control==FALSE and mpre2,sdpre2,n2 are extracted from my aa list when control==TRUE. Two other extraction indices are outcome and post which vary across mpre1... side and the mpre2... side.

这个想法是,在所需输出的每一行中,来自 aa的 mpre1 sdpre1 n1 元素的集合(如下所示)来自 control == FALSE 端( aa )和一组 mpre2 sdpre2 aa 中的 n2 个元素(如下所示)来自 control == TRUE 端.

The idea is that in each row of the desired output, the set of mpre1,sdpre1,n1 elements from aa (see below) come from the control==FALSE side (aa) and the set of mpre2,sdpre2,n2 elements from aa (see below) come from the control==TRUE side.

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

Is my desired output possible to obtain in 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(),以便找到结果 post 的所有组合&提取,然后从中提取那些 control == FALSE ( tlist :用于 mpre1 sdpre1 n1 ),以及那些 control == TRUE ( clist :对于 mpre2 sdpre2 n2 ).

I have tried to form a expand.grid() so all combinations of outcome and post are found & extracted and then among them extract those that are control==FALSE (tlist: for mpre1,sdpre1,n1), and those that are 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

推荐答案

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

这篇关于从BASE R中的两个列表中提取一个data.frame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆