如何在同一列中拆分用逗号分隔的元素? [英] How to split elements separated by commas within the same column?

查看:63
本文介绍了如何在同一列中拆分用逗号分隔的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Google表单,其中包含一些允许多个回复的选项.将表单中的答案转换为Google表格中的电子表格后,这些多个答案将由单元格中的逗号分隔:

I have a Google form with some options that allow multiple responses. When the answers that form are transformed into a spreadsheet in Google Sheets, these multiple answers are separated by commas within the cells:

我想将此电子表格导入RStudio并将数据转换为数据框,以使每个参与者的响应不是用逗号分隔,而是用同一列中的元素分隔(在这种情况下,文档格式[驾驶执照"列] ).

I want to import this spreadsheet into RStudio and transform the data into a data frame so that the responses of each participant are not separated by commas but by elements within the same column (in this case, the document format [driver's license" column ]).

有可能吗?

谢谢!

推荐答案

这是 tidyr 包中 unnest_wider()的工作.

df <- data.frame(document = c("paper, digital", "paper, digital, system", "none"))

df %>% mutate(Separ = strsplit(as.character(document), ",")) %>% 
  tidyr::unnest_wider(Separ)

有输出

# A tibble: 3 x 4
  document               ...1  ...2       ...3     
  <chr>                  <chr> <chr>      <chr>    
1 paper, digital         paper " digital"  NA      
2 paper, digital, system paper " digital" " system"
3 none  

这就是您的初衷吗?

这篇关于如何在同一列中拆分用逗号分隔的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆