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

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

Expand column of Dataframes into existing dataframe (R)

问题

我有一个包含列的数据框(4行 x 3列),其中一列包含数据框(每个数据框有10行 x 3列)。
我的目标是将每个数据框扩展为10行和3列,但保持原始匹配。

大数据框看起来像这样:

  1. 行数:4
  2. 列数:3
  3. $ ID <chr> "A", "B", "C", "D"
  4. $ 报告类型 <chr> "I", "P", "I", "P"
  5. $ 要扩展的列 <list> [<tbl_df[10 x 3>], [<tbl_df[10 x 3>], [<tbl_df[10 x 3>], ...

我的第一个想法是

  1. bind_rows(df$col_to_expand)

但这会丢失ID和报告类型列。

我的目标是最终得到像这样保留信息的结果:

  1. 行数:40
  2. 列数:5
  3. $ ID <chr> "A", "A","A","A" ...
  4. $ 报告类型 <chr> "I", "I", "I", "I"
  5. $ 新列1 <chr> "x", "y" ...
  6. $ 新列2 <chr> "z", "a" ...
  7. $ 新列3 <chr> "t", "u" ...

我最终通过以下方式解决了它:

  1. 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:

  1. Rows: 4
  2. Columns: 3
  3. $ ID &lt;chr&gt; &quot;A&quot; &quot;B&quot; &quot;C&quot; ,&quot;D&quot;
  4. $ report_type &lt;chr&gt; &quot;I&quot;, &quot;P&quot;, &quot;I&quot;, &quot;P&quot;
  5. $ 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

  1. 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:

  1. Rows: 40
  2. Columns: 5
  3. $ ID &lt;chr&gt; &quot;A&quot; &quot;A&quot;,&quot;A&quot;,&quot;A&quot; ...
  4. $ report_type &lt;chr&gt; &quot;I&quot;, &quot;I&quot;, &quot;I&quot;, &quot;I&quot;
  5. $ newco1 &lt;chr&gt; &quot;x&quot;, &quot;y&quot; ...
  6. $ newco2 &lt;chr&gt; &quot;z&quot;, &quot;a&quot; ...
  7. $ newco3 &lt;chr&gt; &quot;t&quot;, &quot;u&quot; ...

I ended up solving it by

  1. 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,您可以执行以下操作:

  1. library(tibble)
  2. library(tidyr)
  3. library(purrr)
  4. set.seed(123)
  5. df <- tibble(
  6. ID = LETTERS[1:4],
  7. report_type = rep(c("I", "P"), 2),
  8. col_to_expand = map(1:4, ~ tibble(
  9. x = sample(letters, 10),
  10. y = sample(letters, 10),
  11. z = sample(letters, 10)))
  12. )
  13. df %>%
  14. unnest(col_to_expand)
英文:

Using tidyr::unnest you could do:

  1. library(tibble)
  2. library(tidyr)
  3. library(purrr)
  4. set.seed(123)
  5. df &lt;- tibble(
  6. ID = LETTERS[1:4],
  7. report_type = rep(c(&quot;I&quot;, &quot;P&quot;), 2),
  8. col_to_expand = map(1:4, ~ tibble(
  9. x = sample(letters, 10),
  10. y = sample(letters, 10),
  11. z = sample(letters, 10)))
  12. )
  13. df |&gt;
  14. unnest(col_to_expand)
  15. #&gt; # A tibble: 40 &#215; 5
  16. #&gt; ID report_type x y z
  17. #&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;
  18. #&gt; 1 A I o s q
  19. #&gt; 2 A I s y k
  20. #&gt; 3 A I n i g
  21. #&gt; 4 A I c c u
  22. #&gt; 5 A I j h l
  23. #&gt; 6 A I r g o
  24. #&gt; 7 A I k j j
  25. #&gt; 8 A I e x m
  26. #&gt; 9 A I x d x
  27. #&gt; 10 A I y n i
  28. #&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:

确定