如何在不同的列中分隔多个选择短语Google表单?

问题描述

我看到有一些关于此问题的主题herehere),但是在两种情况下,示例都具有多个逗号分隔的选择。在这种情况下,有点不同。

  1. 在我的调查中,有选择多个词组的选项(要想弄清楚,其中有些包含逗号)
  2. 一个“其他原因”选项,受访者可以在其中写自己的句子。
  3. 每个句子都以大写字母开头(并且句子中间没有其他大写字母)。除了“其他原因”选项以外,这些选项可以以小写字母开头,具体取决于受访者的书写方式。

预定义选项的列表注册如下:

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")

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...