3

我想要一种将管道操作的结果转换为表格的方法,以便它可以在R Markdown 中呈现为 HTML 表格。

样本数据:

Category <- sample(1:6, 394400) 
Category <- sample(1:6, 394400, replace=TRUE)
Category <- factor(Category,
                    levels = c(1,2,3,4,5,6),
                    labels = c("First",
                               "Second",
                               "Third",
                               "Fourth",
                               "Fifth",
                               "Sixth"))
data <- data.frame(Category)

然后我使用管道构建频率表:

Table <- data %>%
             group_by(Category) %>%
             summarise(N= n(), Percent = n()/NROW(data)*100) %>%
             mutate(C.Percent = cumsum(Percent))

这给了我这个漂亮的小汇总表:

# A tibble: 6 × 4
  Category     N  Percent C.Percent
    <fctr> <int>    <dbl>     <dbl>
1    First 65853 16.69701  16.69701
2   Second 66208 16.78702  33.48403
3    Third 65730 16.66582  50.14985
4   Fourth 65480 16.60243  66.75228
5    Fifth 65674 16.65162  83.40390
6    Sixth 65455 16.59610 100.00000

但是,如果我尝试将其转换为表格然后转换为 HTML,它会告诉我它不能强制Table转换为表格。数据帧也是如此。

有谁知道一种方法,因为我很想自定义输出的外观?

4

1 回答 1

2

There are several packages for that. Here are some:

knitr::kable(Table)

htmlTable::htmlTable(Table)

ztable::ztable(as.data.frame(Table))

DT::datatable(Table)

stargazer::stargazer(Table, type = "html")

Each of these has different customization options.

于 2017-06-12T13:30:49.427 回答