将Dataframes的列扩展到现有的数据框中 (R)

huangapple go评论66阅读模式
英文:

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            &lt;chr&gt; &quot;A&quot; &quot;B&quot; &quot;C&quot; ,&quot;D&quot;
$ report_type   &lt;chr&gt; &quot;I&quot;, &quot;P&quot;, &quot;I&quot;, &quot;P&quot;
$ col_to_expand &lt;list&gt; [&lt;tbl_df[10 x 3]&gt;], [&lt;tbl_df[10 x 3]&gt;], [&lt;tbl_df[10 x 3]&gt;], ...

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            &lt;chr&gt; &quot;A&quot; &quot;A&quot;,&quot;A&quot;,&quot;A&quot; ... 
$ report_type   &lt;chr&gt; &quot;I&quot;, &quot;I&quot;, &quot;I&quot;, &quot;I&quot; 
$ newco1       &lt;chr&gt; &quot;x&quot;, &quot;y&quot; ...
$ newco2       &lt;chr&gt; &quot;z&quot;, &quot;a&quot; ...
$ newco3       &lt;chr&gt; &quot;t&quot;, &quot;u&quot; ...

I ended up solving it by

new_df &lt;- df[rep(seq_len(nrow(df)), each = 10), ] %&gt;% 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 &lt;- tibble(
  ID = LETTERS[1:4],
  report_type = rep(c(&quot;I&quot;, &quot;P&quot;), 2),
  col_to_expand = map(1:4, ~ tibble(
    x = sample(letters, 10), 
    y = sample(letters, 10), 
    z = sample(letters, 10)))
) 

df |&gt; 
  unnest(col_to_expand)
#&gt; # A tibble: 40 &#215; 5
#&gt;    ID    report_type x     y     z    
#&gt;    &lt;chr&gt; &lt;chr&gt;       &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;
#&gt;  1 A     I           o     s     q    
#&gt;  2 A     I           s     y     k    
#&gt;  3 A     I           n     i     g    
#&gt;  4 A     I           c     c     u    
#&gt;  5 A     I           j     h     l    
#&gt;  6 A     I           r     g     o    
#&gt;  7 A     I           k     j     j    
#&gt;  8 A     I           e     x     m    
#&gt;  9 A     I           x     d     x    
#&gt; 10 A     I           y     n     i    
#&gt; # … with 30 more rows

huangapple
  • 本文由 发表于 2023年2月24日 04:44:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550142.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定