R: Extract words from one column to different columns
I figured it out for a couple of hours. Let's say I have a column with 1-4 words separated by a space:
aba bkak absbs
a2aj akls bios
sad
fasa lgk
.
.
.
I would like these words to be in separate columns to make further processing easier. SO instead of these words are in the same column, how can I get them to split the columns?
Thanks for any help.
+3
source to share
2 answers
With devel verison data.table
one can also do
library(data.table) # V >= 1.9.5
setDT(df)[, tstrsplit(V1, ' ')]
# V1 V2 V3
# 1: aba bkak absbs
# 2: a2aj akls bios
# 3: sad NA NA
# 4: fasa lgk NA
Or with stringi
(although you get the matrix back)
library(stringi)
stri_split_fixed(df$V1, ' ', simplify = TRUE)
+4
source to share