我正在使用 summarytools
包中的 freq
函数在 RStudio 中创建频率表。
似乎无法关闭表格中的累积百分比列和总百分比列。例如:
library(summarytools)
data(mtcars)
view(freq(mtcars$cyl, totals=FALSE, cumul=FALSE))
仍然会生成一个包含重复的累积百分比列和总百分比列的表格。我只需要一个包含变量值、计数 # 和百分比的表格。
我已尝试使用 st_options(freq.cumul = FALSE, freq.totals = FALSE)
重置全局选项,但收到一条错误消息:
Error in st_options(freq.cumul = FALSE, freq.totals = FALSE) :
unused arguments (freq.cumul = FALSE, freq.totals = FALSE)
更新
终于弄明白了 - 我没有在 freq
函数中使用足够的参数。以下代码生成了一个不错的频率表:
cyl_freq <- freq(mtcars$cyl, report.nas = FALSE, totals=FALSE, cumul=FALSE, style = "rmarkdown", headings = FALSE);
view(cyl_freq)
如果你需要创建一堆跨多个列的表multiple_:
multiple_freq <- lapply(mtcars[c(2,8:11)], function(x) freq(x, report.nas = FALSE, totals=FALSE, cumul=FALSE, headings = FALSE));
view(multiple_freq)
最佳答案
这没有使用 summarytools
包,但我认为这可能是您要找的东西。
frtable <- table(mtcars$cyl)
percent <- prop.table(frtable)
dt <- cbind(frtable , percent) %>% set_colnames(c("Count", "Percent"))
DT::datatable(dt) %>% DT::formatPercentage('percent')
关于r - 如何从 R 中使用 `freq` 函数生成的表中排除累积和总比例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56839263/