R 使用tbl_摘要获取分类数据的方法
我想为我的分类变量的子集生成均值和频率R 使用tbl_摘要获取分类数据的方法,r,dplyr,gtsummary,R,Dplyr,Gtsummary,我想为我的分类变量的子集生成均值和频率 mtcars2 <- mtcars %>% mutate(across(matches('cyl|gear|carb'), as.factor)) 我知道我可以使用它分别获得分类和连续的输出 mtcars_out <- tbl_summary(mtcars2, statistic = list(all_numeric() ~ "{mean} ({sd})",
mtcars2 <- mtcars %>% mutate(across(matches('cyl|gear|carb'), as.factor))
我知道我可以使用它分别获得分类和连续的输出
mtcars_out <- tbl_summary(mtcars2,
statistic = list(all_numeric() ~ "{mean} ({sd})",
all_categorical() ~ "{n} / {N} ({p}%)")) %>% as_tibble()
由于mtacrs$cyl已具有与其关联的级别,因此我希望使用mtcars2,并生成该变量的平均值。像这样的。。。但tbl_summary不喜欢这样,因为它是一个分类变量
mtcars_out <- tbl_summary(mtcars2,
statistic = list(all_numeric() ~ "{mean} ({sd})",
"cyl"~"{mean} ({sd})")) %>% as_tibble()
Error: Problem with `mutate()` input `tbl_stats`.
x There was an error assembling the summary statistics for 'cyl'
with summary type 'categorical'.
There are 2 common sources for this error.
1. You have requested summary statistics meant for continuous
variables for a variable being as summarized as categorical.
To change the summary type to continuous, add the argument
`type = list(cyl ~ 'continuous')`
2. One of the functions or statistics from the `statistic=` argument is not valid.
i Input `tbl_stats` is `pmap(...)`.
我尝试在调用中指定类型,但也不起作用
mtcars_out <- tbl_summary(mtcars2,
type = list("cyl"~"continuous"),
statistic = list(all_numeric() ~ "{mean} ({sd})",
all_categorical() ~ "{n} / {N} ({p}%)")) %>% as_tibble()
Error: Problem with `mutate()` input `summary_type`.
x Column 'cyl' is class "factor" and cannot be summarized as a continuous variable.
i Input `summary_type` is `assign_summary_type(...)`.
我的实际数据集有500个变量,并且已经为每个变量指定了类,所以我不想更改原始数据集的类类型。我想在tbl_摘要调用中指定它
非常感谢您的帮助 您已将cyl设为一个因子,R不允许您取因子变量的平均值
mtcars_out <- tbl_summary(mtcars2,
statistic = list(all_numeric() ~ "{mean} ({sd})",
"cyl"~"{mean} ({sd})")) %>% as_tibble()
Error: Problem with `mutate()` input `tbl_stats`.
x There was an error assembling the summary statistics for 'cyl'
with summary type 'categorical'.
There are 2 common sources for this error.
1. You have requested summary statistics meant for continuous
variables for a variable being as summarized as categorical.
To change the summary type to continuous, add the argument
`type = list(cyl ~ 'continuous')`
2. One of the functions or statistics from the `statistic=` argument is not valid.
i Input `tbl_stats` is `pmap(...)`.
我认为对你来说,最简单的事情就是得到变量的数值版本和因子版本。从这里你可以总结这两个变量。从那里,您可以删除变量因子版本的额外标题行
mtcars_out <- tbl_summary(mtcars2,
statistic = list(all_numeric() ~ "{mean} ({sd})",
"cyl"~"{mean} ({sd})")) %>% as_tibble()
Error: Problem with `mutate()` input `tbl_stats`.
x There was an error assembling the summary statistics for 'cyl'
with summary type 'categorical'.
There are 2 common sources for this error.
1. You have requested summary statistics meant for continuous
variables for a variable being as summarized as categorical.
To change the summary type to continuous, add the argument
`type = list(cyl ~ 'continuous')`
2. One of the functions or statistics from the `statistic=` argument is not valid.
i Input `tbl_stats` is `pmap(...)`.
图书馆概述
图书馆管理员
待定%
选择Cyl%>%
突变效应周期=因子周期%>%
tbl_摘要
类型=whereis.numeric~连续,
统计=whereis.numeric{mean}{sd},
标签=气缸~气缸数
删除因子变量的额外标题行
tbl$表体%
滤器startsWithvariable,fct_&row_type==标签
打印表
tbl
您已将cyl设为因子,R不允许您取因子变量的平均值
mtcars_out <- tbl_summary(mtcars2,
statistic = list(all_numeric() ~ "{mean} ({sd})",
"cyl"~"{mean} ({sd})")) %>% as_tibble()
Error: Problem with `mutate()` input `tbl_stats`.
x There was an error assembling the summary statistics for 'cyl'
with summary type 'categorical'.
There are 2 common sources for this error.
1. You have requested summary statistics meant for continuous
variables for a variable being as summarized as categorical.
To change the summary type to continuous, add the argument
`type = list(cyl ~ 'continuous')`
2. One of the functions or statistics from the `statistic=` argument is not valid.
i Input `tbl_stats` is `pmap(...)`.
我认为对你来说,最简单的事情就是得到变量的数值版本和因子版本。从这里你可以总结这两个变量。从那里,您可以删除变量因子版本的额外标题行
mtcars_out <- tbl_summary(mtcars2,
statistic = list(all_numeric() ~ "{mean} ({sd})",
"cyl"~"{mean} ({sd})")) %>% as_tibble()
Error: Problem with `mutate()` input `tbl_stats`.
x There was an error assembling the summary statistics for 'cyl'
with summary type 'categorical'.
There are 2 common sources for this error.
1. You have requested summary statistics meant for continuous
variables for a variable being as summarized as categorical.
To change the summary type to continuous, add the argument
`type = list(cyl ~ 'continuous')`
2. One of the functions or statistics from the `statistic=` argument is not valid.
i Input `tbl_stats` is `pmap(...)`.
图书馆概述
图书馆管理员
待定%
选择Cyl%>%
突变效应周期=因子周期%>%
tbl_摘要
类型=whereis.numeric~连续,
统计=whereis.numeric{mean}{sd},
标签=气缸~气缸数
删除因子变量的额外标题行
tbl$表体%
滤器startsWithvariable,fct_&row_type==标签
打印表
tbl
@如果您有任何见解,我将不胜感激@如果您有任何见解,我将不胜感激!