【问题标题】:Separating a column element into 3 separate columns (R)将列元素分成 3 个单独的列 (R)
【发布时间】:2014-08-08 00:06:53
【问题描述】:

我有一个如下的数据框 (theData),其中的值由管道分隔:

Col1  Col2     Col3 
1     colors   red|green|purple
1     colors   red|pink|yellow
1     colors   yellow|mauve|purple
1     colors   red|green|orange
1     colors   red|yellow|purple
1     colors   red|green|purple

我想将 Col3 分成额外的列,如下所示:

Col1     Col2        Col3                    Col4      Col5  
1       colors      red                     green     purple
1       colors      red                     pink      yellow
1       colors      yellow                  mauve     purple
1       colors      red                     green     orange
1       colors      red                     yellow    purple
1       colors      red                     green     purple

我尝试了以下方法:

str_split_fixed(as.character(theData$Col3), "|", 3)

但这不起作用。

【问题讨论】:

  • 可能类似于cbind(theData[, c(1,2 )], as.data.frame(do.call(rbind, strsplit(theData$Col3, "|", fixed=T))))

标签: r


【解决方案1】:

My cSplit function 很容易处理这类问题。

cSplit(theData, "Col3", "|")
#    Col1   Col2 Col3_1 Col3_2 Col3_3
# 1:    1 colors    red  green purple
# 2:    1 colors    red   pink yellow
# 3:    1 colors yellow  mauve purple
# 4:    1 colors    red  green orange
# 5:    1 colors    red yellow purple
# 6:    1 colors    red  green purple

结果是data.table,因为该函数利用“data.table”包来提高效率,尤其是对于较大的数据集。

【讨论】:

    【解决方案2】:

    您也可以从reshape 尝试colsplit

      library(reshape)
      cbind(theData[,1:2],
         colsplit(theData$Col3, "[|]", names=c("Col3", "Col4", "Col5")))
      #  Col1   Col2   Col3   Col4   Col5
     #1    1 colors    red  green purple
     #2    1 colors    red   pink yellow
     #3    1 colors yellow  mauve purple
     #4    1 colors    red  green orange
     #5    1 colors    red yellow purple
     #6    1 colors    red  green purple
    

    或者直接使用read.table

       cbind(theData[,1:2],
             setNames(read.table(text=theData$Col3,sep="|",header=F,stringsAsFactors=F),paste0("Col",3:5)))
    

    【讨论】:

      【解决方案3】:

      再添加一个选项。看你喜不喜欢。这是 Hadley 的 tidyr 包。代码很干净。

      > library(tidyr)
      > test <- data.frame(Col3 = c("red|green|purple", "red|pink|yellow"))
      > test
      Source: local data frame [2 x 1]
      
                    Col3
      1 red|green|purple
      2  red|pink|yellow
      
      > test %>% separate(Col3, c("A", "B", "C"), sep = "\\|")
      Source: local data frame [2 x 3]
      
          A     B      C
      1 red green purple
      2 red  pink yellow
      

      【讨论】:

        【解决方案4】:

        你只需要用[] 包裹|,或者用\\| 转义它。这看起来像是 mapply 的工作。

        > m <- mapply(strsplit, dat$Col3, split = "[|]", USE.NAMES = FALSE)
        > setNames(cbind(dat[-3], do.call(rbind, m)), paste0("Col", 1:5))
        #   Col1   Col2   Col3   Col4   Col5
        # 1    1 colors    red  green purple
        # 2    1 colors    red   pink yellow
        # 3    1 colors yellow  mauve purple
        # 4    1 colors    red  green orange
        # 5    1 colors    red yellow purple
        # 6    1 colors    red  green purple
        

        使用str_split_fixed 的尝试,只需要稍作改动,

        > library(stringr)
        > cbind(dat[-3], str_split_fixed(dat$Col3, "[|]", 3))
        

        【讨论】:

          猜你喜欢
          • 2022-07-13
          • 2022-01-19
          • 2019-02-12
          • 2021-06-15
          • 2018-06-25
          • 2020-08-21
          • 1970-01-01
          • 1970-01-01
          • 2023-01-28
          相关资源
          最近更新 更多