[英]way to create summary table in R with two grouping factors
I am trying to create a summary table in R with two grouping factors based on the following data我正在尝试根据以下数据在 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) is the response variable I am looking to show, and I'd like for it to be split up based on type and species; mean(C) 是我要显示的响应变量,我希望根据类型和物种对其进行拆分; ie type as columns and species as rows
即类型作为列和物种作为行
Every package I've tried to use (xtable, stargazer, gtsummary) doesn't seem to have the ability to do this.我尝试使用的每个软件包(xtable、stargazer、gtsummary)似乎都没有能力做到这一点。 Of course, I can just plug and chug myself, but it'd be nice to know if there is a package.
当然,我可以自己插电,但很高兴知道是否有包裹。 Anyone have any ideas?
有人有想法么?
Thanks so much非常感谢
This is one solution using the tidyr:: package.这是使用 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)
This returns:这将返回:
> 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
If you just want the data frame you presented with the type variable values as columns, you're just looking for the spread
function from the tidyverse.如果您只想要将类型变量值作为列呈现的数据框,那么您只是在 tidyverse 中寻找
spread
函数。
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)
You can add any number of stratifying variables with tbl_strata()
in {gtsummary}.您可以在 {gtsummary} 中使用
tbl_strata()
添加任意数量的分层变量。 Example below!下面的例子!
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"
)
)
Created on 2022-01-07 by the reprex package (v2.0.1)
由reprex 包(v2.0.1) 于 2022 年 1 月 7 日创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.