R 将列表中的每个“data.frame”打印到单独的表中,标题为“data.frame”名称

发布于 2025-01-10 12:45:45 字数 1085 浏览 3 评论 0原文

想要将列表的每个 data.frame 打印到单独的表格中,标题为 data.frame 名称。我尝试的代码如下。它生成表格,但标题错误。

library(tidyverse)
library(kableExtra)
#> 
#> Attaching package: 'kableExtra'
#> The following object is masked from 'package:dplyr':
#> 
#>     group_rows

df1 <- data.frame(X = 1:3)
df2 <- data.frame(Y = 5:6)

ls1 <-
  list(df1, df2) %>% 
  set_names(c("df1", "df2"))

ls1
#> $df1
#>   X
#> 1 1
#> 2 2
#> 3 3
#> 
#> $df2
#>   Y
#> 1 5
#> 2 6

str(ls1)
#> List of 2
#>  $ df1:'data.frame': 3 obs. of  1 variable:
#>   ..$ X: int [1:3] 1 2 3
#>  $ df2:'data.frame': 2 obs. of  1 variable:
#>   ..$ Y: int [1:2] 5 6
names(ls1)
#> [1] "df1" "df2"

imap(
    .x = ls1
  , .f = ~ {
    kbl(
       x       = .x
     , format  = "markdown"
     , caption = names(.x)
    )
  }
)
#> $df1
#> 
#> 
#> Table: X
#> 
#> |  X|
#> |--:|
#> |  1|
#> |  2|
#> |  3|
#> 
#> $df2
#> 
#> 
#> Table: Y
#> 
#> |  Y|
#> |--:|
#> |  5|
#> |  6|

Want to print each data.frame of a list into separate Tables with caption as data.frame name. My attempted code is given below. It produces Tables but with wrong captions.

library(tidyverse)
library(kableExtra)
#> 
#> Attaching package: 'kableExtra'
#> The following object is masked from 'package:dplyr':
#> 
#>     group_rows

df1 <- data.frame(X = 1:3)
df2 <- data.frame(Y = 5:6)

ls1 <-
  list(df1, df2) %>% 
  set_names(c("df1", "df2"))

ls1
#> $df1
#>   X
#> 1 1
#> 2 2
#> 3 3
#> 
#> $df2
#>   Y
#> 1 5
#> 2 6

str(ls1)
#> List of 2
#>  $ df1:'data.frame': 3 obs. of  1 variable:
#>   ..$ X: int [1:3] 1 2 3
#>  $ df2:'data.frame': 2 obs. of  1 variable:
#>   ..$ Y: int [1:2] 5 6
names(ls1)
#> [1] "df1" "df2"

imap(
    .x = ls1
  , .f = ~ {
    kbl(
       x       = .x
     , format  = "markdown"
     , caption = names(.x)
    )
  }
)
#> $df1
#> 
#> 
#> Table: X
#> 
#> |  X|
#> |--:|
#> |  1|
#> |  2|
#> |  3|
#> 
#> $df2
#> 
#> 
#> Table: Y
#> 
#> |  Y|
#> |--:|
#> |  5|
#> |  6|

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丑疤怪 2025-01-17 12:45:45

它应该是.y。如果它是一个命名的列表/向量,则名称默认位于.y中,值位于.x中,这类似于传递map2 中的两个参数。如果没有名称,则 .y 将返回 list

purrr::imap(
  .x = ls1
  , .f = ~ {
    kbl(
      x       = .x
      , format  = "markdown"
      , caption =.y
    )
  }
)  

输出的序列索引

$df1


Table: df1

|  X|
|--:|
|  1|
|  2|
|  3|

$df2


Table: df2

|  Y|
|--:|
|  5|
|  6|

It should be .y. If it is a named list/vector, the names are by default in .y and the values are in .x which is similar to passing two arguments in map2. If there are no names, then the .y will return the sequence index of the list

purrr::imap(
  .x = ls1
  , .f = ~ {
    kbl(
      x       = .x
      , format  = "markdown"
      , caption =.y
    )
  }
)  

-output

$df1


Table: df1

|  X|
|--:|
|  1|
|  2|
|  3|

$df2


Table: df2

|  Y|
|--:|
|  5|
|  6|
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文