我正在尝试在我的数据中准备分类变量的频率分布表,我正在使用下面的代码。但是当我查看输出时输出看起来不错,但在报告中打印不正确。
# 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))
还有其他方法可以准备这样的输出吗?
我希望输出作为每个因子级别的计数表。一列中的每个因子水平,并在另一列中计数。
我正在使用Rmarkdown在word文件中输出。同样在输出窗口中,输出不会打印为两列表。
答案 0 :(得分:1)
要在Markdown中将数据框打印为表,可以使用kable()
中的knitr
函数。
library(knitr)
kable(aDataFrame)
例如......
带有data.frame()
函数的 kable()
对于在R Markdown中传递表格信息非常有用。对于使用这种技术的几个更复杂的例子,请阅读我的文章Commentary on ToothGrowth Factorial ANOVA,在那里我将Robert Kabacoff的分析与约翰霍普金斯大学关于Coursera的统计推断课程的要求进行比较。
的问候,
Len
(11/22/2017)更新:回应来自@ sandhya-ghildiyal的评论,以下是如何从表格输出中排除空白行。如果我们将table()
的结果保存到对象中,我们可以使用[
函数中的提取运算符kable()
来排除因子值为1的行,即空格。
theTable <- as.data.frame(table(STI$Q54))
kable(theTable[as.numeric(theTable$Var1) != 1,])