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

前端 未结 5 1882
不思量自难忘°
不思量自难忘° 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:16

    Here is a nice and simple approach with tidyr.

    library(tidyr)
    
    ncol <- max(sapply(dat, length))
    
    dat %>%
      separate(x, paste0("V", seq(1,ncol)))
    

    Note: You will get a warning, however, it is basically telling you that separate is padding the data with NA's. So you can ignore the warning.

提交回复
热议问题