Frequency distribution of a categorical variable in R

余生长醉 提交于 2019-12-02 03:40:38

问题


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.

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


回答1:


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

library(knitr)
kable(aDataFrame)

For example...

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.

regards,

Len

(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,])


来源:https://stackoverflow.com/questions/47410556/frequency-distribution-of-a-categorical-variable-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!