![](/img/trans.png)
[英]Convert a list of dataframes to a single dataframe with an id column
[英]Get column names and dataframe name from a list of dataframes into a single dataframe
我正在尋找一種方法來將數據框列表中的列名和數據框名稱轉換為單個數據框。 它們的列長度不等。 做到這一點的最佳方法是什么?
dlist <- list(mtcars[1:2], mtcars[1:3], mtcars[1:4])
names(dlist) <- c("mtcars1", "mtcars2", "mtcars3")
Tried:
dlist |> map(~colnames(.x))
預期輸出:
1 mtcars1 mpg cyl NA NA
2 mtcars2 mpg cyl disp NA
3 mtcars3 mpg cyl disp hp
你可以試試:
library(tidyverse)
dlist %>%
map_df(~names(.x) %>%
enframe(), .id = "id") %>%
pivot_wider(names_from = name, id_cols = id)
# A tibble: 3 x 5
id `1` `2` `3` `4`
<chr> <chr> <chr> <chr> <chr>
1 mtcars1 mpg cyl NA NA
2 mtcars2 mpg cyl disp NA
3 mtcars3 mpg cyl disp hp
或在基地相同的想法:
reshape(stack(lapply(dlist, names)), idvar = "ind", timevar = "values", direction = "wide", v.names = "values")
ind values.mpg values.cyl values.disp values.hp
1 mtcars1 mpg cyl <NA> <NA>
3 mtcars2 mpg cyl disp <NA>
6 mtcars3 mpg cyl disp hp
類似於公認的解決方案,但由於unnest_wider
更緊湊:
library(tidyr)
library(tibble)
library(purrr)
dlist %>%
map(colnames) %>%
enframe %>%
unnest_wider(value, names_sep = "_")
#> # A tibble: 3 x 5
#> name value_1 value_2 value_3 value_4
#> <chr> <chr> <chr> <chr> <chr>
#> 1 mtcars1 mpg cyl NA NA
#> 2 mtcars2 mpg cyl disp NA
#> 3 mtcars3 mpg cyl disp hp
也許有更好的方法,但我會這樣做:
library(purrr)
dlist %>%
map(~ .x %>%
names() %>%
append(rep(NA_character_, dlist %>%
map(~ .x %>% names()) %>%
reduce(~ max(length(..1), length(..2))) - length(.x)))) %>%
exec(rbind, !!!.) %>%
as.data.frame()
V1 V2 V3 V4
mtcars1 mpg cyl <NA> <NA>
mtcars2 mpg cyl disp <NA>
mtcars3 mpg cyl disp hp
或者用map2
更簡潔一點,類似於可能親愛的朋友的優雅解決方案:
dlist %>%
map2_dfr(names(dlist), ~ c(.y, names(.x)) %>% set_names(~ letters[seq_along(.x)])) %>%
column_to_rownames("a")
b c d e
mtcars1 mpg cyl <NA> <NA>
mtcars2 mpg cyl disp <NA>
mtcars3 mpg cyl disp hp
基本的 R 解決方案是:
dlist |> lapply(names) |>
(\(x){
res <- t(sapply(x, `length<-`, max(lengths(x))))
cbind(id = names(x), setNames(data.frame(res), 1:NCOL(res)))
})()
#R> id 1 2 3 4
#R> mtcars1 mtcars1 mpg cyl <NA> <NA>
#R> mtcars2 mtcars2 mpg cyl disp <NA>
#R> mtcars3 mtcars3 mpg cyl disp hp
由於行名和id
是相同的,那么這可能會:
dlist |> lapply(names) |>
(\(x) sapply(x, `length<-`, max(lengths(x))))() |>
t() |> as.data.frame()
#R> V1 V2 V3 V4
#R> mtcars1 mpg cyl <NA> <NA>
#R> mtcars2 mpg cyl disp <NA>
#R> mtcars3 mpg cyl disp hp
還是這個?
library(tidyverse)
dlist <- list(mtcars[1:2], mtcars[1:3], mtcars[1:4])
names(dlist) <- c("mtcars1", "mtcars2", "mtcars3")
map_dfr(dlist, ~names(.x) %>% set_names(paste0('col', seq_along(.x))))
#> # A tibble: 3 x 4
#> col1 col2 col3 col4
#> <chr> <chr> <chr> <chr>
#> 1 mpg cyl <NA> <NA>
#> 2 mpg cyl disp <NA>
#> 3 mpg cyl disp hp
如果您還想要返回行名
imap_dfr(dlist, ~c(.y, names(.x)) %>% set_names('cc',paste0('col', seq_along(.x)))) %>%
column_to_rownames('cc')
#> col1 col2 col3 col4
#> mtcars1 mpg cyl <NA> <NA>
#> mtcars2 mpg cyl disp <NA>
#> mtcars3 mpg cyl disp hp
另一種方法是:
do.call('rbind',
lapply(seq_len(length(dlist)),
function(i) c(names(dlist)[i],
names(dlist[[i]]),
rep(NA, max(sapply(dlist, length)) - ncol(dlist[[i]]))
)
)
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.