pander function inside sapply in r
How we use pander()
function inside a sapply()
to knit
a table into HTML/ word?
Here is my code
Data <- data.frame(A = sample(1:2,10,replace = TRUE),
B = sample(1:3,10,replace = TRUE),
C = sample(1:5,10,replace = TRUE),
D = rnorm(10),
E = rnorm(10, 10, 3))
Var_Cat <- 1:3
Var_obsn <- 4:5
sapply(Var_obsn, function(i){
sapply(Var_Cat, function(j){
print(Data[,c(i,j)] |> names())
Data[,c(j,i) ] |>
kruskal.test() |> pander() |> print()})})
Output
I need to use 'rmakdown' to export 开发者_如何转开发my table (many tables at once). So I utilised the 'pander' function within 'sapply'. However, my output is not in a tabular format, so come help me with this.
Thank you
Here is a way using purrr
instead of sapply
:
library(dplyr)
library(purrr)
df <- expand.grid(Var_Cat, Var_obsn) |>
rename(Var_Cat = 1, Var_obsn = 2) |>
purrr::transpose() |>
map_depth(1, bind_rows) |>
map(~ Data |> select(.x$Var_Cat, .x$Var_obsn))
list_names <- df |>
map(colnames) |>
map(~ stringr::str_c(.x, collapse = "_"))
df |>
set_names(list_names) |>
map(kruskal.test) |>
imap(~ broom::tidy(.x) |>
mutate(Var_obsn_Var_Cat = .y) |>
relocate(Var_obsn_Var_Cat, .before = statistic)) |>
bind_rows() |>
kableExtra::kable() |>
kableExtra::kable_styling(bootstrap_options = "striped") |>
kableExtra::kable_minimal()
Output:
精彩评论