【问题标题】:Passing tables into a matrix of lists using a double for-loop使用双 for 循环将表传递到列表矩阵
【发布时间】:2020-07-13 06:44:36
【问题描述】:

我看过 SO(例如 here),但还没有找到可以满足我需要的东西。

我正在学习如何在新的dplyr v1.0.0 中编程,并试图找到一种方法将返回两项列表(其中一项是字符串,另一项是表格)的函数的结果传递到一个二维列表。

这是一个包含三个二元结果变量和五个预测变量的玩具数据集,其中两个是因子。

set.seed(1)
library(dplyr)
df <- tibble(outcome1 = factor(rbinom(10,1, prob = 0.5), levels = 0:1, labels = c("unmet", "met")),
             outcome2 = factor(rbinom(10,1, prob = 0.2), levels = 0:1, labels = c("unmet", "met")),
             outcome3 = factor(rbinom(10,1, prob = 0.8), levels = 0:1, labels = c("unmet", "met")),
             pred1 = rnorm(10),
             pred2 = rnorm(10,5,1),
             pred3 = rnorm(10,15,3),
             pred4 = factor(rep(letters[1:2],5)),
             pred5 = factor(rep(letters[3:4],each=5)))

现在假设我想返回两个因子预测变量中每一个的三个结果变量中未满足与满足的比例。

我可以编写一个dplyr 函数,该函数将为指定预测器的指定结果变量返回未满足与已满足的表格

catFunct_grouped <- function(d, group_var, out_var) {
  d %>% 
    group_by(.data[[group_var]], .data[[out_var]]) %>%
      summarise(count = n()) %>%
        mutate(tot = sum(count),
               perc = round(100*count/tot,2))
}

df %>% catFunct_grouped("pred4", "outcome1")

#output
#   pred4 outcome1 count   tot  perc
#   <fct> <fct>    <int> <int> <dbl>
# 1 a     unmet        2     5    40
# 2 a     met          3     5    60
# 3 b     unmet        2     5    40
# 4 b     met          3     5    60

但是假设我现在想要获得 2 个因子预测变量和三个二元结果的所有 2 x 3 = 6 成对组合?

我尝试创建一个双 for 循环,将六个成对组合(以及一个列出相关结果变量的额外元素)传递到我的函数中,然后传递到一个空列表中。

outNames <- paste0("outcome", 1:3)
predNames <- paste0("pred", 4:5)

grFact <- list()
for (r in 1:length(outNames)) {
  for (c in 1:length(predNames)) {
    grFact[[r]] <- list(outVariable = outNames[r], # prints the outcome name
                        outDF = list(df %>% catFunct_grouped(predNames[c], outNames[r])))
  }
}

但是当我调用新列表时...

grFact

...我得到以下输出

# [[1]]
# [[1]]$outVariable
# [1] "outcome1"
# 
# [[1]]$outDF
# [[1]]$outDF[[1]]
# # A tibble: 4 x 5
# # Groups:   pred5 [2]
#   pred5 outcome1 count   tot  perc
#   <fct> <fct>    <int> <int> <dbl>
# 1 c     unmet        3     5    60
# 2 c     met          2     5    40
# 3 d     unmet        1     5    20
# 4 d     met          4     5    80
# 
# 
# 
# [[2]]
# [[2]]$outVariable
# [1] "outcome2"
# 
# [[2]]$outDF
# [[2]]$outDF[[1]]
# # A tibble: 3 x 5
# # Groups:   pred5 [2]
#   pred5 outcome2 count   tot  perc
# <fct> <fct>    <int> <int> <dbl>
# 1 c     unmet        5     5   100
# 2 d     unmet        4     5    80
# 3 d     met          1     5    20
# 
# 
# 
# [[3]]
# [[3]]$outVariable
# [1] "outcome3"
# 
# [[3]]$outDF
# [[3]]$outDF[[1]]
# # A tibble: 4 x 5
# # Groups:   pred5 [2]
#   pred5 outcome3 count   tot  perc
# <fct> <fct>    <int> <int> <dbl>
# 1 c     unmet        1     5    20
# 2 c     met          4     5    80
# 3 d     unmet        1     5    20
# 4 d     met          4     5    80

...这是在正确的轨道上,但显示了 仅针对第二个预测变量的三个结果中的每一个的满足与未满足的比例。

我假设我需要一个矩阵或列表数组来传递表格,但不确定使用 for 循环执行此操作的语法。

非常感谢任何帮助。

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    由于r 来自1:length(outNames),因此grFact 仅存储长度为3 的列表。请尝试使用嵌套的lapply/map

    unlist(lapply(outNames, function(x) lapply(predNames, function(y) 
           list(outVariable = x, outDF = df %>% catFunct_grouped(x, y)))),
            recursive = FALSE)
    
    
    #[[1]]
    #[[1]]$outVariable
    #[1] "outcome1"
    
    #[[1]]$outDF
    # A tibble: 4 x 5
    # Groups:   outcome1 [2]
    #  outcome1 pred4 count   tot  perc
    #  <fct>    <fct> <int> <int> <dbl>
    #1 unmet    a         2     4    50
    #2 unmet    b         2     4    50
    #3 met      a         3     6    50
    #4 met      b         3     6    50
    
    #[[2]]
    #[[2]]$outVariable
    #[1] "outcome1"
    
    #[[2]]$outDF
    # A tibble: 4 x 5
    # Groups:   outcome1 [2]
    #  outcome1 pred5 count   tot  perc
    #  <fct>    <fct> <int> <int> <dbl>
    #1 unmet    c         3     4  75  
    #2 unmet    d         1     4  25  
    #3 met      c         2     6  33.3
    #4 met      d         4     6  66.7
    #...
    #...
    

    【讨论】:

      猜你喜欢
      • 2019-09-08
      • 2021-05-02
      • 1970-01-01
      • 1970-01-01
      • 2020-09-25
      • 1970-01-01
      • 1970-01-01
      • 2016-04-30
      • 1970-01-01
      相关资源
      最近更新 更多