将字符串拆分为未知数量的新数据帧列

时间:2015-03-11 20:25:49

标签: regex r string

我有一个带有字符列的数据框,其中包含由换行符\n分隔的多个字符串形式的电子邮件元数据:

  person                                                                                                                                                 myString
1   John                                                                                                            To name5@email.com by sender6 on 01-12-2014\n
2   Jane To name@email.com,name4@email.com by sender1 on 01-22-2014\nTo name3@email.com by sender2 on 02-03-2014\nTo email5@domain.com by sender1 on 06-21-2014\n
3    Tim                                                                To name2@email.com by sender2 on 05-11-2014\nTo name@email.com by sender2 on 06-03-2015\n

我想将myString的不同子串分成不同的列,使它看起来像这样:

  person                                                     email1                                      email2                                        email3
1   John                To name5@email.com by sender6 on 01-12-2014                                        <NA>                                          <NA>
2   Jane To name@email.com,name4@email.com by sender1 on 01-22-2014 To name3@email.com by sender2 on 02-03-2014 To email5@domain.com by sender1 on 06-21-2014
3    Tim                To name2@email.com by sender2 on 05-11-2014  To name@email.com by sender2 on 06-03-2015                                          <NA>

我目前的方法是使用tidyr包中的separate

library(dplyr)
library(tidyr)
res1 <- df %>% 
    separate(col = myString, into = paste(rep("email", 3), 1:3), sep = "\\n", extra = "drop")
res1[res1 == ""] <- NA

但是使用这种方法,我必须手动指定要提取三列。

我希望用以下任何一个或两个来改进这个过程:

  1. 一种自动计算分隔符最大出现次数的方法(即需要多少个新变量)
  2. 分割成未知列数的其他方法
  3. 如果有一个很好的解决方案能够以长篇形式返回数据,而不是宽泛的,那也很棒。

    示例数据:

    df <- structure(list(person = c("John", "Jane", "Tim"), myString = c("To name5@email.com by sender6 on 01-12-2014\n", 
        "To name@email.com,name4@email.com by sender1 on 01-22-2014\nTo name3@email.com by sender2 on 02-03-2014\nTo email5@domain.com by sender1 on 06-21-2014\n", 
        "To name2@email.com by sender2 on 05-11-2014\nTo name@email.com by sender2 on 06-03-2015\n"
        )), .Names = c("person", "myString"), row.names = c(NA, -3L), class = "data.frame")
    

4 个答案:

答案 0 :(得分:2)

我会从我的“splitstackshape”软件包中建议cSplit

library(splitstackshape)
cSplit(df, "myString", "\n")
#    person                                                 myString_1
# 1:   John                To name5@email.com by sender6 on 01-12-2014
# 2:   Jane To name@email.com,name4@email.com by sender1 on 01-22-2014
# 3:    Tim                To name2@email.com by sender2 on 05-11-2014
#                                     myString_2
# 1:                                          NA
# 2: To name3@email.com by sender2 on 02-03-2014
# 3:  To name@email.com by sender2 on 06-03-2015
#                                       myString_3
# 1:                                            NA
# 2: To email5@domain.com by sender1 on 06-21-2014
# 3:                                            NA

您也可以使用参数stri_split_fixed从“stringi”包中尝试simplify = TRUE(虽然您的示例数据会在末尾添加一个额外的空列)。方法如下:

library(stringi)
data.frame(person = df$person, 
           stri_split_fixed(df$myString, "\n", 
                            simplify = TRUE))

答案 1 :(得分:1)

看起来很丑陋,但是你走了......

使用strsplit分割char矢量。获取最大长度,将其用于列。

df <- data.frame(
  person = c("John", "Jane", "Tim"),
  myString = c("To name5@email.com by sender6 on 01-12-2014\n",
               "To name@email.com,name4@email.com by sender1 on 01-22-2014\nTo name3@email.com by sender2 on 02-03-2014\nTo email5@domain.com by sender1 on 06-21-2014\n",
               "To name2@email.com by sender2 on 05-11-2014\nTo name@email.com by sender2 on 06-03-2015\n"
  ), stringsAsFactors=FALSE
)

a <- strsplit(df$myString, "\n")
max_len <- max(sapply(a, length))
for(i in 1:max_len){
  df[,paste0("email", i)] <- sapply(a, "[", i)
}

答案 2 :(得分:1)

以下是长篇文章的有效途径:

a <- strsplit(df$myString, "\n")
lens <- vapply(a, length, integer(1L)) # or lengths(a) in R 3.2
longdf <- df[rep(seq_along(a), lens),]
longdf$string <- unlist(a)

请注意stack()通常对这些情况有用。

可以使用IRanges Bioconductor软件包进行简化:

longdf <- df[togroup(a),]
longdf$string <- unlist(a)

然后,如果真的有必要,请转到宽屏:

longdf$myString <- NULL
longdf$token <- sequence(lens)
widedf <- reshape(longdf, timevar="token", idvar="person", direction="wide")

答案 3 :(得分:1)

这可能就足够了:

library(data.table)
dt = as.data.table(df) # or setDT to convert in place

dt[, strsplit(myString, split = "\n"), by = person]
#   person                                                         V1
#1:   John                To name5@email.com by sender6 on 01-12-2014
#2:   Jane To name@email.com,name4@email.com by sender1 on 01-22-2014
#3:   Jane                To name3@email.com by sender2 on 02-03-2014
#4:   Jane              To email5@domain.com by sender1 on 06-21-2014
#5:    Tim                To name2@email.com by sender2 on 05-11-2014
#6:    Tim                 To name@email.com by sender2 on 06-03-2015

然后可以简单地转换为宽格式:

dcast(dt[, strsplit(myString, split = "\n"), by = person][, idx := 1:.N, by = person],
      person ~ idx, value.var = 'V1')
#   person                                                          1                                           2                                             3
#1:   Jane To name@email.com,name4@email.com by sender1 on 01-22-2014 To name3@email.com by sender2 on 02-03-2014 To email5@domain.com by sender1 on 06-21-2014
#2:   John                To name5@email.com by sender6 on 01-12-2014                                          NA                                            NA
#3:    Tim                To name2@email.com by sender2 on 05-11-2014  To name@email.com by sender2 on 06-03-2015                                            NA

# (load reshape2 and use dcast.data.table instead of dcast if not using 1.9.5+)