我有调查数据。有些问题允许多个答案。在我的数据中,不同的答案用逗号分隔。我想在数据框中为每个选择添加一个新行。所以我有这样的事情:
survey$q1 <- c("I like this", "I like that", "I like this, but not much",
"I like that, but not much", "I like this,I like that",
"I like this, but not much,I like that")
如果逗号只是用来划分我会使用的多个选择:
survey <- cSplit(survey, "q1", ",", direction = "long")
并得到想要的结果。鉴于一些逗号是答案的一部分,我尝试使用逗号后跟大写字母作为分隔符:
survey <- cSplit(survey, "q1", ",(?=[A-Z])", direction = "long")
但由于某种原因,它不起作用。它不会给出任何错误,但不会拆分字符串,还会从数据框中删除一些行。然后我尝试使用strsplit:
strsplit(survey$1, ",(?=[A-Z])", perl=T)
它可以正确拆分它,但我无法实现它,以便每个句子成为同一列的不同行,就像 cSplit 一样。所需的输出是:
survey$q1
[1] "I like this"
[2] "I like that"
[3] "I like this, but not much"
[4] "I like that, but not much"
[5] "I like this"
[6] "I like that"
[7] "I like this, but not much"
[8] "I like that"
有没有办法使用这两种方法之一来获得它?谢谢