R中分类变量的频率分布 [英] Frequency distribution of a categorical variable in R

查看:629
本文介绍了R中分类变量的频率分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试准备数据中分类变量的频率分布表,并且正在使用以下代码.但是,当我查看输出时,输出看起来还不错,但在报表中却无法正常打印.

I am trying to prepare a frequency distribution table of a categorical variable in my data and I am using below code. But the output looks ok while I view it but not printing ok in report.

# These lines are not needed because the data below is already
# in that format
# STI<-STI_IPD1%>% select(Q18_1,Q54)
# STI$Q54<-as.factor(STI$Q54)

STI = structure(list(Q18_1 = c(101L, 120L, 29L, 101L, 94L, 16L, 47L, 
141L, 154L, 47L, 141L, 154L, 154L, 29L, 58L, 154L, 101L, 154L, 
47L, 141L, 75L, 1L, 120L, 16L, 154L, 141L, 141L, 154L, 154L, 
154L, 29L, 141L, 38L, 47L, 101L, 16L, 154L, 154L, 101L, 192L, 
58L, 154L, 16L, 120L, 101L, 1L, 38L, 1L, 154L, 1L, 16L, 58L, 
75L, 154L, 47L, 58L, 120L, 141L, 1L, 141L, 16L, 141L, 58L, 29L, 
101L, 58L, 154L, 75L, 75L, 141L, 29L, 101L, 101L, 154L, 16L, 
101L, 101L, 47L, 47L, 181L, 16L, 154L, 47L, 154L, 47L, 120L, 
75L, 47L, 192L, 1L, 154L, 154L, 120L, 141L, 58L, 47L, 154L, 101L, 
75L, 141L, 75L, 16L, 47L, 1L, 58L, 141L), Q54 = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "Discretionary if earnings per share goals are met.", 
"initial funding by targets and as year goes on begin to include financial results", 
"Non-represented are targets focused and budgeted and union plans are self funded based on operating margin achievements."
), class = "factor")), class = c("data.table", "data.frame"), row.names = c(NA, 
-106L), .Names = c("Q18_1", 
"Q54"))

as.data.frame(table(STI$Q54))

还有其他准备此类输出的方法吗?

Is there any other way to prepare such outputs?

我希望输出为每个因子水平的计数表.每一列中的每个因子水平,而另一列中的计数.

I want output as a table of counts of each factor level. each factor level in one column and and counts in another column.

我正在使用Rmarkdown在word文件中获取输出.同样在输出窗口中,输出也不会打印为两列表格.

I am taking output in word file using Rmarkdown. Also in the output window the output is not printing as two columns table.

推荐答案

要在Markdown中将数据框打印为表格,可以在knitr中使用kable()函数.

To print a data frame as a table in Markdown, one can use the kable() function in knitr.

library(knitr)
kable(aDataFrame)

例如...

data.frame()是用于在R Markdown中传递表格信息的非常有用的技术.有关使用此技术的两个更复杂的示例,请阅读我的文章关于牙齿生长因子方差分析"的评论,其中我将罗伯特·卡巴科夫(Robert Kabacoff)的分析与约翰霍普金斯大学(Coursera)的约翰霍普金斯大学统计推断课程的要求进行了比较.

data.frame() with the kable() function is really useful technique for communicating tabular information in R Markdown. For a couple of more complicated examples using this technique, please read my article Commentary on ToothGrowth Factorial ANOVA, where I compare Robert Kabacoff's analysis to the requirements of the Johns Hopkins University Statistical Inference course on Coursera.

致谢

Len

(2017年11月22日)更新:响应@ sandhya-ghildiyal的评论,这是如何从表输出中排除空白行.如果将table()的结果保存到对象中,则可以在kable()函数中使用提取运算符[排除因子值为1的行,即空白.

(11/22/2017) UPDATE: Responding to a comment from @sandhya-ghildiyal , here is how to exclude the blank row from the table output. If we save the result of table() into an object, we can then use the extract operator [ within the kable() function to exclude the row where the factor value is 1, the blank space.

theTable <- as.data.frame(table(STI$Q54))
kable(theTable[as.numeric(theTable$Var1) != 1,])

这篇关于R中分类变量的频率分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆