r - 使用 dplyr group_split 保留数据帧名称

标签 r dplyr

<分区>

使用 dplyr 中的 group_split,但我需要列表中的每个数据框来保留名称。

来自 dplyr 文档的示例(注意数据帧已编号。最佳输出是每个数据帧都具有分组变量的名称(Setosa、versicolor ....):

    ir <- iris %>%
  group_by(Species)

group_split(ir)
#> [[1]]
#> # A tibble: 50 x 5
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>           <dbl>       <dbl>        <dbl>       <dbl> <fct>  
#>  1          5.1         3.5          1.4         0.2 setosa 
#>  2          4.9         3            1.4         0.2 setosa 
#>  3          4.7         3.2          1.3         0.2 setosa 
#>  4          4.6         3.1          1.5         0.2 setosa 
#>  5          5           3.6          1.4         0.2 setosa 
#>  6          5.4         3.9          1.7         0.4 setosa 
#>  7          4.6         3.4          1.4         0.3 setosa 
#>  8          5           3.4          1.5         0.2 setosa 
#>  9          4.4         2.9          1.4         0.2 setosa 
#> 10          4.9         3.1          1.5         0.1 setosa 
#> # … with 40 more rows
#> 
#> [[2]]
#> # A tibble: 50 x 5
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species   
#>           <dbl>       <dbl>        <dbl>       <dbl> <fct>     
#>  1          7           3.2          4.7         1.4 versicolor
#>  2          6.4         3.2          4.5         1.5 versicolor
#>  3          6.9         3.1          4.9         1.5 versicolor
#>  4          5.5         2.3          4           1.3 versicolor
#>  5          6.5         2.8          4.6         1.5 versicolor
#>  6          5.7         2.8          4.5         1.3 versicolor
#>  7          6.3         3.3          4.7         1.6 versicolor
#>  8          4.9         2.4          3.3         1   versicolor
#>  9          6.6         2.9          4.6         1.3 versicolor
#> 10          5.2         2.7          3.9         1.4 versicolor
#> # … with 40 more rows
#> 
#> [[3]]
#> # A tibble: 50 x 5
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species  
#>           <dbl>       <dbl>        <dbl>       <dbl> <fct>    
#>  1          6.3         3.3          6           2.5 virginica
#>  2          5.8         2.7          5.1         1.9 virginica
#>  3          7.1         3            5.9         2.1 virginica
#>  4          6.3         2.9          5.6         1.8 virginica
#>  5          6.5         3            5.8         2.2 virginica
#>  6          7.6         3            6.6         2.1 virginica
#>  7          4.9         2.5          4.5         1.7 virginica
#>  8          7.3         2.9          6.3         1.8 virginica
#>  9          6.7         2.5          5.8         1.8 virginica
#> 10          7.2         3.6          6.1         2.5 virginica
#> # … with 40 more rows
#> 
#> attr(,"ptype")
#> # A tibble: 0 x 5
#> # … with 5 variables: Sepal.Length <dbl>, Sepal.Width <dbl>,
#> #   Petal.Length <dbl>, Petal.Width <dbl>, Species <fct>

最佳答案

group_split 不保留名称。来自 ?group_split

it does not name the elements of the list based on the grouping as this typically loses information and is confusing.

你可以使用 base base::split

split(iris, iris$Species)

或者使用 setNames 单独命名 tibbles 列表。

library(dplyr)
group_split(ir) %>% setNames(unique(iris$Species))

group_split 根据数据的因子级别进行拆分,因此如果我们想根据它们在数据中的出现来拆分它们,我们可能必须重新排列因子级别。在 iris 数据集中,因子水平的顺序与它们在数据中出现的顺序相同,因此上述方法有效。

更一般的我们应该使用。

iris %>%
  mutate(Species= factor(Species, levels = unique(Species))) %>%
  group_split(Species) %>%
  setNames(unique(iris$Species))

关于r - 使用 dplyr group_split 保留数据帧名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57475399/

相关文章:

按组删除特定行号/条件以下的行

r - 错误 "no applicable method for ' 重组'应用于类 "c(' 整数的对象', 'numeric' )""

r - 使用 rvest 抓取 wiki 可折叠 table

r - 如何过滤具有多个条件的行?

r - 在列标题和成对变量中嵌入数据的数据透视表

r - 在条件下将 which.min 应用于 data.table

r - 使用 blogdown 后 Hugo 不读取 .Rmd 文件

r - 在 R 中使用 dplyr 将列对相乘

r - "binning"行进入范围 (dplyr/R)

r - 为具有特定条件的标志创建 id 序列