【问题标题】:pander function inside sapply in rr 中 sapply 中的 pander 函数
【发布时间】:2022-12-07 14:13:28
【问题描述】:

我们如何使用pander()函数将sapply()knit表格转换成HTML/word?

这是我的代码

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

我需要使用“降价”来导出我的表(一次导出很多表)。所以我利用了“lapply”中的“pander”功能。但是,我的输出不是表格格式,所以请帮我解决这个问题。

谢谢

【问题讨论】:

    标签: r r-markdown apply pander


    【解决方案1】:
    # Load the knitr package
    library(knitr)
    
    # Define the data frame
    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))
    
    # Define the variables for the table
    Var_Cat <- 1:3
    Var_obsn <- 4:5
    
    # Use sapply() to apply the pander() and kable() functions to each combination of variables
    sapply(Var_obsn, function(i){
      sapply(Var_Cat, function(j){
        # Convert the data to a formatted table using pander() and kable()
        Data[,c(j,i)] |> kruskal.test() |> pander() |> kable()
      })
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多