折叠列表列元素到字符串

发布于 2025-02-12 11:17:35 字数 1583 浏览 1 评论 0原文

我想将一个子类别折叠成一个逗号的字符串以进行显示。
这就是我所拥有的。

library(tibble)
eats <- tribble(
  ~food,~variety,
  "fruit","cherry",
  "fruit","apple",
  "fruit","peach",
  "nut","cashew",
  "nut","almonds"
)
eats
#> # A tibble: 5 x 2
#>   food  variety
#>   <chr> <chr>  
#> 1 fruit cherry 
#> 2 fruit apple  
#> 3 fruit peach  
#> 4 nut   cashew 
#> 5 nut   almonds

这就是我想要的:

eats2 <- tribble(
  ~food,~varieties,
  "fruit","cherry, apple, peach",
  "nut","cashew, almond"
)
eats2  
#> # A tibble: 2 x 2
#>   food  varieties           
#>   <chr> <chr>               
#> 1 fruit cherry, apple, peach
#> 2 nut   cashew, almond

我尝试的是:

eats %>% 
  nest(data=variety) %>% 
  mutate(data = paste(data,collapse = ""))
#> # A tibble: 2 x 2
#>   food  data                                                                    
#>   <chr> <chr>                                                                   
#> 1 fruit "list(variety = c(\"cherry\", \"apple\", \"peach\"))list(variety = c(\"~
#> 2 nut   "list(variety = c(\"cherry\", \"apple\", \"peach\"))list(variety = c(\"~

不。

eats %>% 
  nest(data=variety) %>% 
  map(~.x %>% mutate(varieties=paste(data,collapse = "")))
#> Error in UseMethod("mutate"): no applicable method for 'mutate' applied to an object of class "character"

由Reprex软件包(v2.0.1)在2022-07-01创建

。正确的方法是什么?谢谢。

I would like to collapse a subcategory into a comma-delimited string for display.
This is what I have.

library(tibble)
eats <- tribble(
  ~food,~variety,
  "fruit","cherry",
  "fruit","apple",
  "fruit","peach",
  "nut","cashew",
  "nut","almonds"
)
eats
#> # A tibble: 5 x 2
#>   food  variety
#>   <chr> <chr>  
#> 1 fruit cherry 
#> 2 fruit apple  
#> 3 fruit peach  
#> 4 nut   cashew 
#> 5 nut   almonds

This is what I want:

eats2 <- tribble(
  ~food,~varieties,
  "fruit","cherry, apple, peach",
  "nut","cashew, almond"
)
eats2  
#> # A tibble: 2 x 2
#>   food  varieties           
#>   <chr> <chr>               
#> 1 fruit cherry, apple, peach
#> 2 nut   cashew, almond

This what I've tried:

eats %>% 
  nest(data=variety) %>% 
  mutate(data = paste(data,collapse = ""))
#> # A tibble: 2 x 2
#>   food  data                                                                    
#>   <chr> <chr>                                                                   
#> 1 fruit "list(variety = c(\"cherry\", \"apple\", \"peach\"))list(variety = c(\"~
#> 2 nut   "list(variety = c(\"cherry\", \"apple\", \"peach\"))list(variety = c(\"~

Nope.

eats %>% 
  nest(data=variety) %>% 
  map(~.x %>% mutate(varieties=paste(data,collapse = "")))
#> Error in UseMethod("mutate"): no applicable method for 'mutate' applied to an object of class "character"

Created on 2022-07-01 by the reprex package (v2.0.1)

Also nope. What is the right way to do this? Thanks.

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

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

发布评论

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

评论(2

风为裳 2025-02-19 11:17:35

您可以使用总结toString

library(tibble)
library(dplyr)
eats <- tribble(
  ~food,~variety,
  "fruit","cherry",
  "fruit","apple",
  "fruit","peach",
  "nut","cashew",
  "nut","almonds"
)
eats %>%
  group_by(food) %>%
  summarise(varieties = toString(variety)) %>%
  ungroup()
#> # A tibble: 2 × 2
#>   food  varieties           
#>   <chr> <chr>               
#> 1 fruit cherry, apple, peach
#> 2 nut   cashew, almonds

在2022-07-01创建的 reprex软件包(v2.0.1)

You can use the following code using summarise and toString:

library(tibble)
library(dplyr)
eats <- tribble(
  ~food,~variety,
  "fruit","cherry",
  "fruit","apple",
  "fruit","peach",
  "nut","cashew",
  "nut","almonds"
)
eats %>%
  group_by(food) %>%
  summarise(varieties = toString(variety)) %>%
  ungroup()
#> # A tibble: 2 × 2
#>   food  varieties           
#>   <chr> <chr>               
#> 1 fruit cherry, apple, peach
#> 2 nut   cashew, almonds

Created on 2022-07-01 by the reprex package (v2.0.1)

夜血缘 2025-02-19 11:17:35

就像 @quinten的解决方案一样,只需使用粘贴而不是toString

library(dplyr)

eats %>%
  group_by(food) %>%
  summarise(varieties = paste(variety, collapse = ", ")) %>%
  ungroup()
  food  varieties           
  <chr> <chr>               
1 fruit cherry, apple, peach
2 nut   cashew, almonds

Like @Quinten's solution, just using paste instead of toString:

library(dplyr)

eats %>%
  group_by(food) %>%
  summarise(varieties = paste(variety, collapse = ", ")) %>%
  ungroup()
  food  varieties           
  <chr> <chr>               
1 fruit cherry, apple, peach
2 nut   cashew, almonds
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文