0

我正在尝试根据以下数据在 R 中创建一个包含两个分组因素的汇总表

   species        plot   type  `mean(C)`
   <fct>        <fct> <fct>     <dbl>
 1 CA          MI  A         -35.7
 2 CA          MI  B         -35.6
 3 CA          MI  C         -35.9
 4 FO          MI  A         -35.7
 5 FO          MI  B         -34.9
 6 FO          MI  C         -35.3
 7 HE          MI  A         -35.4
 8 HE          MI  B         -35.6
 9 HE          MI  C         -35.6
10 LA          MI  A         -36.5

mean(C) 是我要显示的响应变量,我希望根据类型和物种对其进行拆分;即类型作为列和物种作为行

我尝试使用的每个软件包(xtable、stargazer、gtsummary)似乎都没有能力做到这一点。当然,我可以自己插电,但很高兴知道是否有包裹。有人有想法么?

非常感谢

4

3 回答 3

0

如果您只想要以类型变量值作为列呈现的数据框,那么您只需spread从 tidyverse 中寻找函数。

library(tidyr)

df <- data.frame(species = c("CA", "CA", "CA", "FO", "FO", "FO", "HE", "HE", "HE", "LA"),
                 plot = "MI", 
                 type = c("A", "B", "C", "A", "B", "C", "A", "B", "C", "A"),
                 mean_C = c(-35.7,-35.6,-35.9, -35.7,-34.9,-35.3, -35.4,-35.6, -35.6,-36.5))


new_df <- df %>%
  spread(type, mean_C)

print(new_df)
于 2020-10-22T20:19:33.817 回答
0

这是使用 tidyr:: 包的一种解决方案。

library(tidyr)
# define your data as dataframe df
df <- data.frame("species" = c("CA","CA","CA","FO","FO", "FO", "HE", "HE", "HE", "LA"),
                         "plot" = c("MI"),
                         "type" = c("A", "B", "C", "A", "B", "C", "A", "B", "C", "A"),
                         "mean" = c(-35.7, -35.6, -35.9, -35.7, -34.9, -35.3, -35.4, -35.6, -35.6, -36.5))
# pivot df around 'type', using 'mean'   
df %>%
  pivot_wider(names_from = type, values_from = mean)

这将返回:

> df %>%
+   pivot_wider(names_from = type, values_from = mean)
# A tibble: 4 x 5
species plot      A     B     C   
<fct>   <fct> <dbl> <dbl> <dbl> 
1 CA      MI    -35.7 -35.6 -35.9 
2 FO      MI    -35.7 -34.9 -35.3 
3 HE      MI    -35.4 -35.6 -35.6 
4 LA      MI    -36.5  NA    NA
于 2020-10-22T20:15:03.220 回答
0

tbl_strata()您可以在 {gtsummary}中添加任意数量的分层变量。下面的例子!

library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.5.0'

tbl <-
  trial %>%
  mutate(grade = paste("Grade", grade)) %>%
  tbl_strata(
    strata = grade,
    ~ .x %>%
      tbl_summary(
        by = trt, 
        include = c(age, response),
        missing = "no"
      )
  )

在此处输入图像描述 reprex 包于 2022-01-07 创建(v2.0.1)

于 2022-01-08T00:52:59.273 回答