如何解决如何在不同的列中分隔多个选择短语Google表单?
我看到有一些关于此问题的主题(here和here),但是在两种情况下,示例都具有多个逗号分隔的选择。在这种情况下,有点不同。
- 在我的调查中,有选择多个词组的选项(要想弄清楚,其中有些包含逗号)
- 有一个“其他原因”选项,受访者可以在其中写自己的句子。
- 每个句子都以大写字母开头(并且句子中间没有其他大写字母)。除了“其他原因”选项以外,这些选项可以以小写字母开头,具体取决于受访者的书写方式。
预定义选项的列表注册如下:
Q1.list <- c ("Phrase one without comma","Phrase two also without comma","Phrase three,with comma")
数据库如下:
Q1
"Phrase one without comma,Phrase two also without comma"
"Phrase two also without comma,Phrase three,with comma"
"Phrase three,with comma,Phrase four,other reasons"
"Phrase one without comma,other reasons,Phrase five other reasons"
我想以这种方式转换数据集:
Q1.1 Q1.2 Q1.3 Others
1 1 0 0
0 1 1 0
0 0 1 "Phrase four,other reasons"
1 0 0 "Phrase four,Phrase five other reasons [and everything else that is not on the Q1.list]"
有人可以阐明如何解决这个问题吗?
解决方法
您可以使用dplyr&co。并执行以下操作。
library(dplyr)
library(stringr)
data %>%
transmute(Q1.1 = +(str_detect(Q1,Q1.list[1])),Q1.2 = +(str_detect(Q1,Q1.list[2])),Q1.3 = +(str_detect(Q1,Q1.list[3])),Others = str_remove_all(Q1,str_c(Q1.list,collapse = '|')),Others = if_else(str_sub(Others,1,2) == ',',str_sub(Others,3),Others),Others = if_else(Others == '','0',Others))
# Q1.1 Q1.2 Q1.3 Others
# <int> <int> <int> <chr>
# 1 1 1 0 0
# 2 0 1 1 0
# 3 0 0 1 Phrase four,other reasons
# 4 1 0 0 Phrase four,other reasons,Phrase five other reasons
数据
data <- structure(list(Q1 = c("Phrase one without comma,Phrase two also without comma","Phrase two also without comma,Phrase three,with comma","Phrase three,with comma,Phrase four,other reasons","Phrase one without comma,Phrase five other reasons"
)),row.names = c(NA,-4L),class = c("tbl_df","tbl","data.frame"
))
Q1.list <- c("Phrase one without comma","Phrase two also without comma",with comma")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。