Splitting text column into ragged multiple new columns in a data table in R

后端 未结 5 1678
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-06 07:58

I have a data table containing 20000+ rows and one column. The string in each column has different number of words. I want to split the words and put each of them in a new c

5条回答
  •  醉梦人生
    2020-12-06 08:48

    Check out cSplit from my "splitstackshape" package. It works on either data.frames or data.tables (but always returns a data.table).

    Assuming KFB's sample data is at least slightly representative of your actual data, you can try:

    library(splitstackshape)
    cSplit(df, "x", " ")
    #     x_1      x_2         x_3 x_4
    # 1: This       is interesting  NA
    # 2: This actually          is not
    

    Another (blazing) option is to use stri_split_fixed with simplify = TRUE (from "stringi") (which is obviously deemed to enter the "splitstackshape" code soon):

    library(stringi)
    stri_split_fixed(df$x, " ", simplify = TRUE)
    #      [,1]   [,2]       [,3]          [,4] 
    # [1,] "This" "is"       "interesting" NA   
    # [2,] "This" "actually" "is"          "not"
    

提交回复
热议问题