strsplit by row and distribute results by column in data.frame

前端 未结 5 1891
不思量自难忘°
不思量自难忘° 2021-02-20 15:38

So I have the data.frame

dat = data.frame(x = c(\'Sir Lancelot the Brave\', \'King Arthur\',  
                       \'The Black Knight\', \'The Rabbit\'), stri         


        
5条回答
  •  失恋的感觉
    2021-02-20 16:43

    This is an old question, I know, but I thought I would share two additional options.

    Option 1

    concat.split from my "splitstackshape" package was designed exactly for this type of thing.

    library(splitstackshape)
    concat.split(dat, "x", " ")
    #                        x  x_1      x_2    x_3   x_4
    # 1 Sir Lancelot the Brave  Sir Lancelot    the Brave
    # 2            King Arthur King   Arthur             
    # 3       The Black Knight  The    Black Knight      
    # 4             The Rabbit  The   Rabbit        
    

    Option 2

    data.table has recently (as of version 1.8.11, I believe) had some additions to its arsenal, notably in this case dcast.data.table. To use it, unlist the split data (as was done in @mnel's answer), create a "time" variable using .N (how many new values per row), and use dcast.data.table to transform the data into the form you are looking for.

    library(data.table)
    library(reshape2)
    packageVersion("data.table")
    # [1] ‘1.8.11’
    
    DT <- data.table(dat)
    S1 <- DT[, list(X = unlist(strsplit(x, " "))), by = seq_len(nrow(DT))]
    S1[, Time := sequence(.N), by = seq_len]
    dcast.data.table(S1, seq_len ~ Time, value.var="X")
    #    seq_len    1        2      3     4
    # 1:       1  Sir Lancelot    the Brave
    # 2:       2 King   Arthur     NA    NA
    # 3:       3  The    Black Knight    NA
    # 4:       4  The   Rabbit     NA    NA
    

提交回复
热议问题