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

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

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

问题

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

library(dplyr)
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 %>%
  rowwise() %>%
  reframe(C1, C2, C3) #我想要的输出

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

my_data %>%
  rowwise() %>%
  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.

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

my_data |&gt; 
  rowwise() |&gt; 
  reframe(C1, C2, C3) #The output I would like to get
#&gt; # A tibble: 4 &#215; 3
#&gt;      C1    C2    C3
#&gt;   &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
#&gt; 1     1     5     9
#&gt; 2     2     6    10
#&gt; 3     3     7    11
#&gt; 4     4     8    12

my_data |&gt; 
  rowwise() |&gt; 
  reframe(everything()) #Would like to find a way to reframe all the columns
#&gt; Error in `reframe()`:
#&gt; ℹ In argument: `everything()`.
#&gt; ℹ In row 1.
#&gt; Caused by error:
#&gt; ! `everything()` must be used within a *selecting* function.
#&gt; ℹ See &lt;https://tidyselect.r-lib.org/reference/faq-selection-context.html&gt; for
#&gt;   details.

答案1

得分: 2

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

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,

<details>
<summary>英文:</summary>

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())


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. 

</details>



# 答案2
**得分**: 1

Instead of `dplyr::reframe`, you could use `tidyr::unnest_longer` like this:

``` r
library(tidyr)

my_data |>
  tidyr::unnest_longer(everything())
#> # A tibble: 4 × 3
#>      C1    C2    C3
#>   <dbl> <dbl> <dbl>
#> 1     1     5     9
#> 2     2     6    10
#> 3     3     7    11
#> 4     4     8    12
英文:

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

library(tidyr)

my_data |&gt;
  tidyr::unnest_longer(everything())
#&gt; # A tibble: 4 &#215; 3
#&gt;      C1    C2    C3
#&gt;   &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
#&gt; 1     1     5     9
#&gt; 2     2     6    10
#&gt; 3     3     7    11
#&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:

确定