在 R 中取消嵌套列表和连接

编程入门 行业动态 更新时间:2024-10-26 12:22:56
本文介绍了在 R 中取消嵌套列表和连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我希望在 tibble 中取消嵌套(扁平化?)并连接文本字符串(逗号分隔).示例数据:

I wish to unnest (flatten?) and concatenate strings (comma separated) of text within a tibble. Example data:

library(tidyverse)

tibble(person = c("Alice", "Bob", "Mary"), 
          score = list(c("Red", "Green", "Blue"), c("Orange", "Green", "Yellow"), "Blue"))

# A tibble: 3 x 2
  person score    
  <chr>  <list>   
1 Alice  <chr [3]>
2 Bob    <chr [3]>
3 Mary   <chr [1]>

预期输出:

tibble(person = c("Alice", "Bob", "Mary"),
       score = c("Red, Green, Blue", "Orange, Green, Yellow", "Blue" ))

# A tibble: 3 x 2
  person score                
  <chr>  <chr>                
1 Alice  Red, Green, Blue     
2 Bob    Orange, Green, Yellow
3 Mary   Blue   

我怀疑有一个非常简洁的 tidyverse 解决方案,但经过广泛搜索后我一直无法找到答案;我怀疑我使用了错误的搜索词(unnest/concatenate).tidyverse 解决方案将是首选.谢谢.

I suspect there's a very neat tidyverse solution to this but I've been unable to find an answer after extensive searching; I suspect I'm using the wrong search terms (unnest/concatentate). A tidyverse solution would be preferred. Thank you.

推荐答案

一种简单的方法是将长格式数据unnest 并按组折叠.

A simple way would be to unnest the data in long format and collapse it by group.

library(dplyr)

df %>%
  tidyr::unnest(score) %>%
  group_by(person) %>%
  summarise(score = toString(score))

# person score                
#  <chr>  <chr>                
#1 Alice  Red, Green, Blue     
#2 Bob    Orange, Green, Yellow
#3 Mary   Blue        

<小时>

其他选项是 rowwise

df %>% rowwise() %>% mutate(score = toString(score))

这篇关于在 R 中取消嵌套列表和连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 13:28:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1394539.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!