data.table (version 1.14.6)

tstrsplit: strsplit and transpose the resulting list efficiently


This is equivalent to transpose(strsplit(...)). This is a convenient wrapper function to split a column using strsplit and assign the transposed result to individual columns. See examples.


tstrsplit(x, ..., fill=NA, type.convert=FALSE, keep, names=FALSE)


A transposed list after splitting by the pattern provided.



The vector to split (and transpose).


All the arguments to be passed to strsplit.


Default is NA. It is used to fill shorter list elements so as to return each element of the transposed result of equal lengths.


TRUE calls type.convert with on the columns.


Specify indices corresponding to just those list elements to retain in the transposed result. Default is to return all.


TRUE auto names the list with V1, V2 etc. Default (FALSE) is to return an unnamed list.


It internally calls strsplit first, and then transpose on the result.

names argument can be used to return an auto named list, although this argument does not have any effect when used with :=, which requires names to be provided explicitly. It might be useful in other scenarios.

See Also

data.table, transpose


Run this code
x = c("abcde", "ghij", "klmnopq")
strsplit(x, "", fixed=TRUE)
tstrsplit(x, "", fixed=TRUE)
tstrsplit(x, "", fixed=TRUE, fill="")

# using keep to return just 1,3,5
tstrsplit(x, "", fixed=TRUE, keep=c(1,3,5))

# names argument
tstrsplit(x, "", fixed=TRUE, keep=c(1,3,5), names=LETTERS[1:3])

DT = data.table(x=c("A/B", "A", "B"), y=1:3)
DT[, c("c1") := tstrsplit(x, "/", fixed=TRUE, keep=1L)][]
DT[, c("c1", "c2") := tstrsplit(x, "/", fixed=TRUE)][]

Run the code above in your browser using DataCamp Workspace