
Last chance! 50% off unlimited learning
Sale ends in
Splitting a string into a number of sections of defined length is needed, when we want to split a table given as a number of lines without separator into columns. The cutting points can either be defined by the lengths of the sections or directly by position.
StrChop(x, len, pos)
a vector with the parts of the string.
the string to be cut in pieces.
a vector with the lengths of the pieces.
a vector of cutting positions. Will be ignored when len
has been defined.
Andri Signorell <andri@signorell.net>
If length is going over the end of the string the last part will be returned, so if the rest of the string is needed, it's possible to simply enter a big number as last partlength.
len
and pos
can't be defined simultaneously, only alternatively.
Typical usages are
StrChop(x, len)
StrChop(x, pos)
FixToTable
, StrLeft
, substr
x <- paste(letters, collapse="")
StrChop(x=x, len = c(3,5,2))
# and with the rest integrated
StrChop(x=x, len = c(3, 5, 2, nchar(x)))
# cutpoints at 5th and 10th position
StrChop(x=x, pos=c(5, 10))
Run the code above in your browser using DataLab