【问题标题】:t.test outputs in the `table` package in RR中`table`包中的t.test输出
【发布时间】:2021-08-14 14:46:43
【问题描述】:

以下是我正在处理的数据示例:

> dput(candidateEvokeDFYoung)
structure(list(youngTreatment = structure(c(NA, 1, 0, 1, 0, 1, 
0, 1, 1, 0, 1, 1, 0, 0, NA, NA, NA, NA, 1, 1), format.stata = "%10.0g"), 
    candTrustworthy = structure(c(0, 0, 0, 0, 0, 0, 0, 1, 0, 
    1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), format.stata = "%10.0g"), 
    candKnowledgeable = structure(c(1, 0, 0, 0, 1, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1), format.stata = "%10.0g"), 
    candQualified = structure(c(0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 1), format.stata = "%10.0g"), 
    candConservative = structure(c(0, 0, 0, 0, 1, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0), format.stata = "%10.0g"), 
    candLiberal = structure(c(0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0), format.stata = "%10.0g"), candInexperienced = structure(c(0, 
    1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0), format.stata = "%10.0g"), 
    candPrincipled = structure(c(1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 
    0, 0, 0, 1, 1, 1, 0, 0, 0, 0), format.stata = "%10.0g"), 
    candDistance = structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 1, 0, 0, 0), format.stata = "%10.0g"), 
    candEfficacy = structure(c(1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 1, 0, 0, 1, 0, 0), format.stata = "%10.0g")), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))

我想要做的是使用带有 t.test 结果的表包生成一个表。我遇到的麻烦是我已经获取了这个数据集并使用 lapply 计算了我对每个变量的 t.tests,其中 youngTreatment 作为我的“y”变量:


    candidateEvokesDiffYoung = lapply(candidateEvokeDFYoung[-1], function(x) t.test(x ~ candidateEvokeDFYoung$youngTreatment))

这给了我一个列表列表。我不知道如何使用 tables::tabular 访问


    list[['statistic']]


    list[['p.value]]

我当然可以自己手动将所有这些都拉出来,并将其放入 stargazer 或其他东西的数据框中,但我想知道是否有人知道我可以如何更有效地使用表格包来做到这一点。

【问题讨论】:

    标签: r list


    【解决方案1】:

    t.test 返回类 htest 的对象。我相信收集htest 类对象结果的最佳方法是使用包broom 的函数tidy

    library(broom)
    candidateEvokesDiffYoung = lapply(candidateEvokeDFYoung[-1], 
                               function(x) {
                                       t.test(x ~ candidateEvokeDFYoung$youngTreatment)
    })
    m <- t(sapply(candidateEvokesDiffYoung, tidy))
    

    这将允许您以类似于您尝试的方式引用元素。

    > m["candTrustworthy",  "p.value"][[1]]
    [1] 0.7875872
    
    > unlist(m[,  "p.value"])
    candTrustworthy candKnowledgeable     candQualified  candConservative       candLiberal candInexperienced    candPrincipled      candDistance      candEfficacy                                                                                         
    0.7875872         0.7875872            0.7875872         0.3632175           0.3465935         0.6933006         0.3790778               NaN         0.3632175
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-06
      • 2013-04-07
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多