如何分隔不同列中的多项选择短语(谷歌表单)?
Posted
技术标签:
【中文标题】如何分隔不同列中的多项选择短语(谷歌表单)?【英文标题】:How to separate multiple choice phrases (Google Forms) in different columns? 【发布时间】:2020-10-06 16:11:54 【问题描述】:我看到有一些关于这个问题的主题(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, Phrase four, 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, other reasons, Phrase five other reasons [and everything else that is not on the Q1.list]"
有人能解释一下如何解决这个问题吗?
【问题讨论】:
【参考方案1】:您可以使用 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 four, other reasons, 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", "Phrase three, with comma")
【讨论】:
以上是关于如何分隔不同列中的多项选择短语(谷歌表单)?的主要内容,如果未能解决你的问题,请参考以下文章
多对多字段的 Django 表单上的逗号分隔文本而不是多项选择