仅当子串是另一个向量的元素时,才用逗号分隔字符串

时间:2014-08-02 18:42:49

标签: r string strsplit

我有一组调查回复,受访者可以选择零个或多个选项来回答问题"您喜欢什么类型的水果?"。还有一个写入答案的空间。在结果电子表格中,每个人的回答都在一个单元格中,不同类型的水果用逗号分隔,如下所示:

(df <- data.frame(id = c("A", "B", "C", "D", "E"), 
                 data = c("oranges, apples, peaches, cherries, pineapples, strawberries",
                          "oranges, peaches, pears", 
                          "pears, nectarines, cherries (bing, rainier)", 
                          "apples, peaches, nectarines", 
                          ""), 
                 stringsAsFactors = FALSE))

#   id                                                         data
# 1  A oranges, apples, peaches, cherries, pineapples, strawberries
# 2  B                                      oranges, peaches, pears
# 3  C                  pears, nectarines, cherries (bing, rainier)
# 4  D                                  apples, peaches, nectarines
# 5  E  

我想要做的是将响应分成长格式表格,我使用底部的代码几乎完成了。但是,有些受访者在写入回复中包含了逗号,我不想将其答案分成逗号。我知道所有原始的多项选择是什么; 我怎么能只拆分这些答案,让写入(用逗号)保持原样?我想最终得到这样的数据框:

   id                               data
1   A                            oranges
2   A                             apples
3   A                            peaches
4   A cherries, pineapples, strawberries
5   B                            oranges
6   B                            peaches
7   B                              pears
8   C                              pears
9   C                         nectarines
10  C           cherries (bing, rainier)
11  D                             apples
12  D                            peaches
13  D                         nectarines

多项选择是:

mc_answers <- c("oranges", "plums", "apples", "peaches", "pears", "nectarines")

到目前为止,我所做的是:

# use strsplit to create a list of the types of fruit each person likes
datalist <- strsplit(df$data, ", ")
names(datalist) <- df$id

# remove zero-length list elements (person E doesn't like any fruit)
datalist <- Filter(length, datalist)

# convert list elements to data frames
datalist_dfs <- lapply(datalist, data.frame, stringsAsFactors = FALSE)
datalist_dfs <- lapply(datalist_dfs, setNames, "data") # name each column 'data'

# add id column to each data frame
data_long <- mapply(function(x, y) "[<-"(x, "id", value = y), datalist_dfs, 
                    names(datalist_dfs), SIMPLIFY = FALSE)

# combine into one big data frame
(data_per_person <- do.call('rbind', data_long))
#               data id
# A.1        oranges  A
# A.2         apples  A
# A.3        peaches  A
# A.4       cherries  A   # should
# A.5     pineapples  A   # be one
# A.6   strawberries  A   # entry
# B.1        oranges  B
# B.2        peaches  B
# B.3          pears  B
# C.1          pears  C
# C.2     nectarines  C
# C.3 cherries (bing  C   # should be 
# C.4       rainier)  C   # one entry
# D.1         apples  D
# D.2        peaches  D
# D.3     nectarines  D

没有关于一个人可以选择多少水果的规则,但如果有写入答案,它总是最后一次。

3 个答案:

答案 0 :(得分:3)

这一行之后:

datalist <- Filter(length, datalist)

执行:

datalist <- lapply(datalist, function(x) {
   if(any(!x %in% mc_answers))
       c(x[x %in% mc_answers], paste(x[!x %in% mc_answers], collapse = ", "))
   else
       x[x %in% mc_answers]
})

然后按原样运行其余代码,最终得到:

> (data_per_person <- do.call('rbind', data_long))
                                  data id
A.1                            oranges  A
A.2                             apples  A
A.3                            peaches  A
A.4 cherries, pineapples, strawberries  A
B.1                            oranges  B
B.2                            peaches  B
B.3                              pears  B
C.1                              pears  C
C.2                         nectarines  C
C.3           cherries (bing, rainier)  C
D.1                             apples  D
D.2                            peaches  D
D.3                         nectarines  D

答案 1 :(得分:3)

您也可以尝试:

 library(data.table)
 library(devtools)
 source_gist(11380733) ## 

 df1 <- cSplit(df, "data", sep=", ", "long")
 indx <- df1$data %in% mc_answers
 res <- rbindlist(list(df1[indx,], df1[!indx,][, list(data=paste(data, collapse=", ")), by=id]))[order(id)]

  res
  #   id                               data
  #1:  A                            oranges
  #2:  A                             apples
  #3:  A                            peaches
  #4:  A cherries, pineapples, strawberries
  #5:  B                            oranges
  #6:  B                            peaches
  #7:  B                              pears
  #8:  C                              pears
  #9:  C                         nectarines
 #10:  C           cherries (bing, rainier)
 #11:  D                             apples
 #12:  D                            peaches
 #13:  D                         nectarines

答案 2 :(得分:2)

这样的事情

do.call(rbind, lapply(split(df, df$id), function(x) {
    v<-unlist(strsplit(x$data, ",\\s?"))
    v<-c(v[v %in% mc_answers], paste(v[!v %in% mc_answers], collapse=", "))
    v<-v[nchar(v)>0]
    if (length(v)>0) {
        data.frame(id=x$id[1], data=v)
    } else {
        NULL
    }
}))

这里我们分开处理每个组,然后进行字符串拆分。然后我们将折叠所有不在mc_answers向量中的条目。它返回

    id                               data
A.1  A                            oranges
A.2  A                             apples
A.3  A                            peaches
A.4  A cherries, pineapples, strawberries
B.1  B                            oranges
B.2  B                            peaches
B.3  B                              pears
C.1  C                              pears
C.2  C                         nectarines
C.3  C           cherries (bing, rainier)
D.1  D                             apples
D.2  D                            peaches
D.3  D                         nectarines