tstrsplit

Published by onesixx on

equivalent to transpose(strsplit(...))

 

 

> strsplit("A text I want to display with spaces", NULL) 
[[1]]
 [1] "A" " " "t" "e" "x" "t" " " "I" " " "w" "a" "n" "t" " " "t" "o" " " "d" "i" "s" "p" "l" "a" "y" " " "w" "i" "t" "h" " " "s" "p" "a" "c" "e" "s"
> strsplit("A text I want to display with spaces", NULL) %>% transpose()
[[1]]
[1] "A"

[[2]]
[1] " "

[[3]]
[1] "t"


[[36]]
[1] "s"
> noquote(strsplit("A text I want to display with spaces", NULL)[[1]])
 [1] A   t e x t   I   w a n t   t o   d i s p l a y   w i t h   s p a c e s

 

 

transpose(l, fill=NA, ignore.empty=FALSE)
dt = data.table(x=1:5, y=6:10)
> dt
   x  y
1: 1  6
2: 2  7
3: 3  8
4: 4  9
5: 5 10

> transpose(dt)
   V1 V2 V3 V4 V5
1:  1  2  3  4  5
2:  6  7  8  9 10

 

 

 

 

 to split a column using strsplit 
and assign the transposed result to individual columns

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

x   Split하거나 transpose할 벡터 

Details
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.

Value
A transposed list after splitting by the pattern provided.

See Also
data.table, transpose

Examples
x = c(“abcde”, “ghij”, “klmnopq”)
strsplit(x, “”, fixed=TRUE)
tstrsplit(x, “”, fixed=TRUE)
tstrsplit(x, “”, fixed=TRUE, fill=”<NA>”)

# 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)][]

 

Categories: Reshaping

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x