如何在数据框中“重新框架”所有列?

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

How to `reframe` all the columns in a dataframe?

问题

你想要将包含所有列表列的tibble重新构造,而无需指定所有列的名称。以下是一个示例。my_data包含所有列表列,我想使用reframe将其转换为常规数据框。你可以通过指定所有列的名称来实现这一点。

  1. library(dplyr)
  2. my_data <- tibble(C1 = list(c(1,2,3,4)),
  3. C2 = list(c(5,6,7,8)),
  4. C3 = list(c(9,10,11,12)))
  5. my_data %>%
  6. rowwise() %>%
  7. reframe(C1, C2, C3) #我想要的输出

要重新构造所有列,你可以尝试以下方式:

  1. my_data %>%
  2. rowwise() %>%
  3. reframe(across(everything()))

这将重新构造所有列,无需指定列名。

英文:

Hello I would like to reframe all the columns in a tibble that contains all list columns without having to specify all the columns by name. Below contains an example. my_data is all list columns and I want to turn it into a regular dataframe using reframe. I can do this by specifying all the names of the columns.

  1. library(dplyr)
  2. my_data &lt;- tibble(C1 = list(c(1,2,3,4)),
  3. C2 = list(c(5,6,7,8)),
  4. C3 = list(c(9,10,11,12)))
  5. my_data |&gt;
  6. rowwise() |&gt;
  7. reframe(C1, C2, C3) #The output I would like to get
  8. #&gt; # A tibble: 4 &#215; 3
  9. #&gt; C1 C2 C3
  10. #&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
  11. #&gt; 1 1 5 9
  12. #&gt; 2 2 6 10
  13. #&gt; 3 3 7 11
  14. #&gt; 4 4 8 12
  15. my_data |&gt;
  16. rowwise() |&gt;
  17. reframe(everything()) #Would like to find a way to reframe all the columns
  18. #&gt; Error in `reframe()`:
  19. #&gt; ℹ In argument: `everything()`.
  20. #&gt; ℹ In row 1.
  21. #&gt; Caused by error:
  22. #&gt; ! `everything()` must be used within a *selecting* function.
  23. #&gt; ℹ See &lt;https://tidyselect.r-lib.org/reference/faq-selection-context.html&gt; for
  24. #&gt; details.

答案1

得分: 2

我猜你可以使用tidyverse中的unnest()函数来将列表列展开成一个常规数据框。你可以使用unnest()和everything()来展开所有列:

  1. library(tidyverse)
  2. my_data <- tibble(C1 = list(c(1,2,3,4)),
  3. C2 = list(c(5,6,7,8)),
  4. C3 = list(c(9,10,11,
  5. <details>
  6. <summary>英文:</summary>
  7. I guess you can probably do this using unnest() from tidyverse to flatten the list columns into a regular dataframe. You can use unnest() with everything() to unnest all columns:

library(tidyverse)

my_data <- tibble(C1 = list(c(1,2,3,4)),
C2 = list(c(5,6,7,8)),
C3 = list(c(9,10,11,12)))

my_data %>%
unnest(everything())

  1. This function will return a new dataframe where all the list columns have been unnested. You can use it on any dataframe with list columns without having to specify each column by name. The `unnest(everything())` line of code is unnesting all columns.
  2. </details>
  3. # 答案2
  4. **得分**: 1
  5. Instead of `dplyr::reframe`, you could use `tidyr::unnest_longer` like this:
  6. ``` r
  7. library(tidyr)
  8. my_data |>
  9. tidyr::unnest_longer(everything())
  10. #> # A tibble: 4 × 3
  11. #> C1 C2 C3
  12. #> <dbl> <dbl> <dbl>
  13. #> 1 1 5 9
  14. #> 2 2 6 10
  15. #> 3 3 7 11
  16. #> 4 4 8 12
英文:

Instead of dplyr::reframe you could use tidyr::unnest_longer like so:

  1. library(tidyr)
  2. my_data |&gt;
  3. tidyr::unnest_longer(everything())
  4. #&gt; # A tibble: 4 &#215; 3
  5. #&gt; C1 C2 C3
  6. #&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
  7. #&gt; 1 1 5 9
  8. #&gt; 2 2 6 10
  9. #&gt; 3 3 7 11
  10. #&gt; 4 4 8 12

huangapple
  • 本文由 发表于 2023年7月18日 06:32:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76708480.html
匿名

发表评论

匿名网友

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

确定