简体   繁体   中英

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:

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.

Thank you.

Did you try using the map() function from the purrr package?

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)

Personnaly, I do:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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