简体   繁体   English

如何在 R 的 sapply 中使用 kable

[英]How to use kable inside sapply in R

My goal is to tabulate multiple columns at the same time and I used the following:我的目标是同时将多个列制成表格,我使用了以下内容:

sapply(iris[1:4], table)

Next I tried to use kable to format the output, but I keep getting an error:接下来我尝试使用 kable 格式化 output,但我一直收到错误:

sapply(iris[1:4], knitr::kable(table))
Error in seq_len(m) : argument must be coercible to non-negative integer

So I'd like to know if it is possible to use kable inside sapply.所以我想知道是否可以在 sapply 中使用 kable。

Thank you.谢谢你。

Did you try using the map() function from the purrr package?您是否尝试使用 purrr package 中的 map() function?

This function applies any function to each element of a list (here, you have a data frame so it will apply the function to each column)此 function 将任何 function 应用于列表的每个元素(这里,您有一个数据框,因此它将应用 function 到每一列)

Personnaly, I do:就个人而言,我这样做:

map(iris[1:4], function(.x){

        .x %>% knitr::kable() 
        #You can include any styling options after
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM