将字符串拆分为R中的新行

时间:2014-08-20 09:42:20

标签: r split dataframe strsplit

我有一个如下数据集:

Country Region    Molecule      Item Code   
    IND     NA       PB102      FR206985511 
   THAI     AP       PB103      BA-107603 / F000113361 / 107603
   LUXE     NA       PB105      1012701 / SGP-1012701 / F041701000
    IND     AP       PB106      AU206985211 / CA-F206985211
   THAI     HP       PB107      F034702000 / 1010701 / SGP-1010701
   BANG     NA       PB108      F000007970/25781/20009021

我想基于ITEMCODE的{​​{1}}列中的字符串值进行拆分,并为每个条目创建一个新行。

例如,所需的输出将是:

/

我尝试了以下代码

Country Region Molecule      Item.Code
    IND     NA    PB102    FR206985511
   THAI     AP    PB103      BA-107603
   THAI     AP    PB103     F000113361
   THAI     AP    PB103         107603
   LUXE     NA    PB105        1012701
   LUXE     NA    PB105    SGP-1012701
   LUXE     NA    PB105     F041701000
    IND     AP    PB106    AU206985211
    IND     AP    PB106  CA-F206985211
   THAI     HP    PB107     F034702000
   THAI     HP    PB107        1010701
   THAI     HP    PB107    SGP-1010701
   BANG     NA    PB108     F000007970
   BANG     NA    PB108          25781
   BANG     NA    PB108       20009021

但得到了错误

library(splitstackshape)
df2=concat.split.multiple(df1,"Plant.Item.Code","/", direction="long")

当我尝试"Error: memory exhausted (limit reached?)" 时,我收到以下错误消息。

strsplit()

3 个答案:

答案 0 :(得分:16)

尝试使用cSplit函数(因为您已经使用了@Anandas包)。请注意,它将返回data.table对象,因此请确保已安装此软件包。您可以通过data.frame

之类的内容恢复setDF(df2)(如果您愿意)
library(splitstackshape)
df2 <- cSplit(df1, "Item.Code", sep = "/", direction = "long")
df2
#     Country Region Molecule      Item.Code
#  1:     IND     NA    PB102    FR206985511
#  2:    THAI     AP    PB103      BA-107603 
#  3:    THAI     AP    PB103     F000113361 
#  4:    THAI     AP    PB103         107603
#  5:    LUXE     NA    PB105        1012701 
#  6:    LUXE     NA    PB105    SGP-1012701 
#  7:    LUXE     NA    PB105     F041701000
#  8:     IND     AP    PB106    AU206985211 
#  9:     IND     AP    PB106  CA-F206985211
# 10:    THAI     HP    PB107     F034702000 
# 11:    THAI     HP    PB107        1010701 
# 12:    THAI     HP    PB107    SGP-1010701
# 13:    BANG     NA    PB108     F000007970
# 14:    BANG     NA    PB108          25781
# 15:    BANG     NA    PB108       20009021

答案 1 :(得分:6)

基础R中的另一种方法:

as.data.frame(do.call(rbind, apply(df1, 1, function(x) {
      do.call(expand.grid, strsplit(x, " */ *"))
})))

结果:

   Country Region Molecule     Item.Code
1      IND   <NA>    PB102   FR206985511
2     THAI     AP    PB103     BA-107603
3     THAI     AP    PB103    F000113361
4     THAI     AP    PB103        107603
5     LUXE   <NA>    PB105       1012701
6     LUXE   <NA>    PB105   SGP-1012701
7     LUXE   <NA>    PB105    F041701000
8      IND     AP    PB106   AU206985211
9      IND     AP    PB106 CA-F206985211
10    THAI     HP    PB107    F034702000
11    THAI     HP    PB107       1010701
12    THAI     HP    PB107   SGP-1010701
13    BANG   <NA>    PB108    F000007970
14    BANG   <NA>    PB108         25781
15    BANG   <NA>    PB108      20009021

答案 2 :(得分:2)

尝试这样的事情

d <- structure(list(Country = c("A", "B", "C"), `Item Code` = c("FR206985511", 
    "BA-107603/F000113361/107603", "1012701/SGP-1012701/F041701000")),
    .Names = c("Country", "Item Code"), row.names = c(NA, -3L),
    class = "data.frame")
d
#   Country                      Item code
#         A                    FR206985511
#         B    BA-107603/F000113361/107603
#         C 1012701/SGP-1012701/F041701000

codes <- strsplit(d$"Item Code", "/")
code.lengths <- sapply(codes, length)
new.d <- d[rep(1:nrow(d), code.lengths), ]
new.d$"Item Code" <- unlist(codes)
new.d 
#    Country   Item Code
#1         A FR206985511
#2         B   BA-107603
#2.1       B  F000113361
#2.2       B      107603
#3         C     1012701
#3.1       C SGP-1012701
#3.2       C  F041701000

如果你想摆脱空间(你的原始数据似乎包含),你可以通过d$"Item Code" <- gsub(" ", "", d$"Item Code")

来做到这一点。