本文介绍了在 R 中取消嵌套列表和连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在 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 中取消嵌套列表和连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 06:25