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

后端 未结 5 1682
爱一瞬间的悲伤
爱一瞬间的悲伤 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:24

    OK for both data.table and data.frame

    # toy data
    df <- structure(list(x = structure(c(2L, 1L), .Label = c("This actually is not", 
    "This is interesting"), class = "factor")), .Names = "x", row.names = c(NA, 
    -2L), class = "data.frame")
    
    #                      x
    # 1  This is interesting
    # 2 This actually is not
    
    # the code
    split_result <- strsplit(as.character(df$x), " ")
    length_n <- sapply(split_result, length)
    length_max <- seq_len(max(length_n))
    as.data.frame(t(sapply(split_result, "[", i = length_max))) # Or as.data.table(...)
    
    #     V1       V2          V3   V4
    # 1 This       is interesting 
    # 2 This actually          is  not
    

提交回复
热议问题