如何将tibble中的向量分组到单个向量中?

发布于 2025-02-13 21:00:48 字数 1254 浏览 0 评论 0原文


tibble(x = rep(1:3, 2),
       y = list(1:5, 1:10, 10:20, 20:40, 1:50, 5:10)) -> df

df
#> # A tibble: 6 × 2
#>       x y         
#>   <int> <list>    
#> 1     1 <int [5]> 
#> 2     2 <int [10]>
#> 3     3 <int [11]>
#> 4     1 <int [21]>
#> 5     2 <int [50]>
#> 6     3 <int [6]>

我想分组'x',然后将每个组的向量总结成一个向量。我尝试使用C(),但没有帮助。

df %>% 
  group_by(x) %>% 
  summarise(z = c(y))
#> `summarise()` has grouped output by 'x'. You can override using the `.groups`
#> argument.
#> # A tibble: 6 × 2
#> # Groups:   x [3]
#>       x z         
#>   <int> <list>    
#> 1     1 <int [5]> 
#> 2     1 <int [21]>
#> 3     2 <int [10]>
#> 4     2 <int [50]>
#> 5     3 <int [11]>
#> 6     3 <int [6]>

我还希望在一个组或任何其他类似功能中应用于这些类型的数据集中的元素结合。

df %>% 
  group_by(x) %>% 
  summarise(z = union(y))
#> Error in `summarise()`:
#> ! Problem while computing `z = union(y)`.
#> ℹ The error occurred in group 1: x = 1.
#> Caused by error in `base::union()`:
#> ! argument "y" is missing, with no default


tibble(x = rep(1:3, 2),
       y = list(1:5, 1:10, 10:20, 20:40, 1:50, 5:10)) -> df

df
#> # A tibble: 6 × 2
#>       x y         
#>   <int> <list>    
#> 1     1 <int [5]> 
#> 2     2 <int [10]>
#> 3     3 <int [11]>
#> 4     1 <int [21]>
#> 5     2 <int [50]>
#> 6     3 <int [6]>

I want to group_by 'x' and summmarise the vectors of each group into a single vector. I tried using c(), but it didn't help.

df %>% 
  group_by(x) %>% 
  summarise(z = c(y))
#> `summarise()` has grouped output by 'x'. You can override using the `.groups`
#> argument.
#> # A tibble: 6 × 2
#> # Groups:   x [3]
#>       x z         
#>   <int> <list>    
#> 1     1 <int [5]> 
#> 2     1 <int [21]>
#> 3     2 <int [10]>
#> 4     2 <int [50]>
#> 5     3 <int [11]>
#> 6     3 <int [6]>

I also want a union of elements in a group or any other similar function applied to these kinds of datasets.

df %>% 
  group_by(x) %>% 
  summarise(z = union(y))
#> Error in `summarise()`:
#> ! Problem while computing `z = union(y)`.
#> ℹ The error occurred in group 1: x = 1.
#> Caused by error in `base::union()`:
#> ! argument "y" is missing, with no default

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荆棘i 2025-02-20 21:00:48

如果您希望数据保持嵌套,则可以执行

df %>% 
  group_by(x) %>% 
  summarise(z = list(unlist(y)))

c()函数无法正常工作,因为它不会命名。例如,比较

c(list(1:3, 4:5))
unlist(list(1:3, 4:5))

c函数不会返回一个向量。但是UNLIST确实如此。这很重要,因为当您使用总结时,您的功能将收到匹配行值列表。

另请注意,如果您删除list(),则不再嵌套值

df %>% 
  group_by(x) %>% 
  summarise(z = unlist(y))
#        x     z
#    <int> <int>
#  1     1     1
#  2     1     2
#  3     1     3
#  4     1     4
#  5     1     5
#  6     1    20
#  7     1    21
#  ...

If you want the data to remain nested, you can do

df %>% 
  group_by(x) %>% 
  summarise(z = list(unlist(y)))

The c() function won't work because it' doesn't unnest-lists. For example, compare

c(list(1:3, 4:5))
unlist(list(1:3, 4:5))

The c function doesn't return a single vector. But unlist does. This matters because your function will recieve a list of matching row values when you use summarize.

Also note that if you leave off the list(), the values don't be nested anymore

df %>% 
  group_by(x) %>% 
  summarise(z = unlist(y))
#        x     z
#    <int> <int>
#  1     1     1
#  2     1     2
#  3     1     3
#  4     1     4
#  5     1     5
#  6     1    20
#  7     1    21
#  ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文