tstrsplit.Rd
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)
x | The vector to split (and transpose). |
---|---|
... | All the arguments to be passed to |
fill | Default is |
type.convert |
|
keep | Specify indices corresponding to just those list elements to retain in the transposed result. Default is to return all. |
names |
|
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.
A transposed list after splitting by the pattern provided.
#> [[1]] #> [1] "a" "b" "c" "d" "e" #> #> [[2]] #> [1] "g" "h" "i" "j" #> #> [[3]] #> [1] "k" "l" "m" "n" "o" "p" "q" #>tstrsplit(x, "", fixed=TRUE)#> [[1]] #> [1] "a" "g" "k" #> #> [[2]] #> [1] "b" "h" "l" #> #> [[3]] #> [1] "c" "i" "m" #> #> [[4]] #> [1] "d" "j" "n" #> #> [[5]] #> [1] "e" NA "o" #> #> [[6]] #> [1] NA NA "p" #> #> [[7]] #> [1] NA NA "q" #>tstrsplit(x, "", fixed=TRUE, fill="<NA>")#> [[1]] #> [1] "a" "g" "k" #> #> [[2]] #> [1] "b" "h" "l" #> #> [[3]] #> [1] "c" "i" "m" #> #> [[4]] #> [1] "d" "j" "n" #> #> [[5]] #> [1] "e" "<NA>" "o" #> #> [[6]] #> [1] "<NA>" "<NA>" "p" #> #> [[7]] #> [1] "<NA>" "<NA>" "q" #>#> [[1]] #> [1] "a" "g" "k" #> #> [[2]] #> [1] "c" "i" "m" #> #> [[3]] #> [1] "e" NA "o" #>#> $A #> [1] "a" "g" "k" #> #> $B #> [1] "c" "i" "m" #> #> $C #> [1] "e" NA "o" #>DT = data.table(x=c("A/B", "A", "B"), y=1:3) DT[, c("c1") := tstrsplit(x, "/", fixed=TRUE, keep=1L)][]#> x y c1 #> 1: A/B 1 A #> 2: A 2 A #> 3: B 3 B#> x y c1 c2 #> 1: A/B 1 A B #> 2: A 2 A <NA> #> 3: B 3 B <NA>