英文:
Expand column of Dataframes into existing dataframe (R)
问题
我有一个包含列的数据框(4行 x 3列),其中一列包含数据框(每个数据框有10行 x 3列)。
我的目标是将每个数据框扩展为10行和3列,但保持原始匹配。
大数据框看起来像这样:
行数:4
列数:3
$ ID <chr> "A", "B", "C", "D"
$ 报告类型 <chr> "I", "P", "I", "P"
$ 要扩展的列 <list> [<tbl_df[10 x 3>], [<tbl_df[10 x 3>], [<tbl_df[10 x 3>], ...
我的第一个想法是
bind_rows(df$col_to_expand)
但这会丢失ID和报告类型列。
我的目标是最终得到像这样保留信息的结果:
行数:40
列数:5
$ ID <chr> "A", "A","A","A" ...
$ 报告类型 <chr> "I", "I", "I", "I"
$ 新列1 <chr> "x", "y" ...
$ 新列2 <chr> "z", "a" ...
$ 新列3 <chr> "t", "u" ...
我最终通过以下方式解决了它:
new_df <- df[rep(seq_len(nrow(df)), each = 10), ] %>% bind_cols(df$col_to_expand)
但我想知道是否有人有一个更干净的方法,适用于列中数据框的行数不一致的情况。
谢谢!
英文:
I have a dataframe (4 obs x 3 variables) with a column that contains dataframes (10 obs x 3 variables each)
My goal is to expand each dataframe into 10 rows and 3 new columns but keeping the original matching.
The big dataframe looks like this:
Rows: 4
Columns: 3
$ ID <chr> "A" "B" "C" ,"D"
$ report_type <chr> "I", "P", "I", "P"
$ col_to_expand <list> [<tbl_df[10 x 3]>], [<tbl_df[10 x 3]>], [<tbl_df[10 x 3]>], ...
My first idea was
bind_rows(df$col_to_expand)
but that loses the ID and report type columns.
My goal is to end up with something like this that preserves information:
Rows: 40
Columns: 5
$ ID <chr> "A" "A","A","A" ...
$ report_type <chr> "I", "I", "I", "I"
$ newco1 <chr> "x", "y" ...
$ newco2 <chr> "z", "a" ...
$ newco3 <chr> "t", "u" ...
I ended up solving it by
new_df <- df[rep(seq_len(nrow(df)), each = 10), ] %>% bind_cols(df$col_to_expand)
but I was wondering if anyone had a cleaner way for a case where the number of rows in the column of dataframes is not consistent.
Thanks!
答案1
得分: 2
使用 tidyr::unnest
,您可以执行以下操作:
library(tibble)
library(tidyr)
library(purrr)
set.seed(123)
df <- tibble(
ID = LETTERS[1:4],
report_type = rep(c("I", "P"), 2),
col_to_expand = map(1:4, ~ tibble(
x = sample(letters, 10),
y = sample(letters, 10),
z = sample(letters, 10)))
)
df %>%
unnest(col_to_expand)
英文:
Using tidyr::unnest
you could do:
library(tibble)
library(tidyr)
library(purrr)
set.seed(123)
df <- tibble(
ID = LETTERS[1:4],
report_type = rep(c("I", "P"), 2),
col_to_expand = map(1:4, ~ tibble(
x = sample(letters, 10),
y = sample(letters, 10),
z = sample(letters, 10)))
)
df |>
unnest(col_to_expand)
#> # A tibble: 40 × 5
#> ID report_type x y z
#> <chr> <chr> <chr> <chr> <chr>
#> 1 A I o s q
#> 2 A I s y k
#> 3 A I n i g
#> 4 A I c c u
#> 5 A I j h l
#> 6 A I r g o
#> 7 A I k j j
#> 8 A I e x m
#> 9 A I x d x
#> 10 A I y n i
#> # … with 30 more rows
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论