0

我正在尝试在 R 中处理来自 Google 表单的结果,并且在处理字符串数据时遇到了困难。

这个问题可以在这里看到:

在此处输入图像描述

Google 在单个列中返回结果,每个响应用逗号分隔。

他们最终看起来像

ID | Type of Research
=====================
1  | Policy analysis, Review of other research
2  | Bla
3  | Review of other research, Original empirical research
4  | Policy analysis, Theoretical 
5  | Review of other research

我使用 grepl 为三个预先选择的响应创建逻辑列和 data.frame。

Private$ResearchTypeOriginal <- grepl("Original", Private$ResearchType)
Private$ResearchTypeReview <- grepl("Review", Private$ResearchType)
Private$ResearchTypePolicy <- grepl("Policy", Private$ResearchType)

ResearchTypeGrid <- data.frame(Private$ResearchTypeOriginal, Private$ResearchTypeReview, Private$ResearchTypePolicy)

这很好用。但是,我还需要拔出“其他”。我正在使用

ResearchTypeOther <- subset(Private, !grepl("Original", Private$ResearchType) & !grepl("Review", Private$ResearchType) & !grepl("Policy", Private$ResearchType), select=c(ID, ResearchType, PubLang, Reviewer))
ResearchTypeOther <- na.omit(ResearchTypeOther)

但刚刚意识到,如果一个响应既有预选响应又有开放式响应,则使用此方法会丢失。它可以很好地给我“Bla”回复,但只有那些完全是“其他”的回复。

换句话说,这会产生

ID |  Type of Research
=======================
2  |  Bla 

但我想要的是

ID |  Type of Research
======================
2  |  Bla
4  |  Policy analysis, Theoretical

这是我第一次在 SO 上发帖,而且我显然是 R 的新手,所以请原谅我提出问题的方式有任何错误。如果我的措辞不好,我很抱歉。我有大约 20 个其他问题有同样的问题,所以我需要一个灵活的解决方案。

谢谢你的帮助。

4

3 回答 3

0

多亏了卢克。一点也不优雅,但这有效:

items <- c("Review of other research", 
           "Original empirical research", 
           "Policy analysis")
ResearchTypeOther <- data.frame((others <- gsub(sprintf("(,\\s)?(%s)(,\\s)?", paste(items, collapse = "|")), "", 
           sub(".*\\|\\s(.*)", "\\1", Private$ResearchType))))
ResearchTypeOther[ResearchTypeOther==""] <- NA
ResearchTypeOther <- na.omit(ResearchTypeOther)
于 2014-08-17T14:14:26.663 回答
0

您可以按照以下方式“通过正则表达式”

doc <- readLines(n = 5)
1  | Policy analysis, Review of other research
2  | Bla
3  | Review of research, Original empirical research
4  | Policy analysis, Theoretical 
5  | Review of other research

items <- c("Review of other research", 
           "Original empirical research", 
           "Policy analysis")
(others <- gsub(sprintf("(,\\s)?(%s)(,\\s)?", paste(items, collapse = "|")), "", 
           sub(".*\\|\\s(.*)", "\\1", doc)))
# [1] ""                   "Bla"                "Review of research"
# [4] "Theoretical "       ""  


sub(sprintf("(,\\s)?(%s)(,\\s)?", paste(others[others != ""], collapse = "|")), "", doc)
# [1] "1  | Policy analysis, Review of other research"
# [2] "2  | "                                         
# [3] "3  | Original empirical research"              
# [4] "4  | Policy analysis"                          
# [5] "5  | Review of other research" 
于 2014-08-17T12:39:09.803 回答
0

您可以尝试:(使用docitems来自@lukeA)

 library(stringr)
 doc[sapply(strsplit(doc, "\\d +\\||,"), function(x) {
                 x1 <- str_trim(x)
                 x2 <- x1[x1!='']
                 indx <- x2 %in% items
                 !(any(indx) & tail(indx,1))})]
  #[1] "2  | Bla"                            "4  | Policy analysis, Theoretical 
于 2014-08-17T14:16:17.033 回答