Data Science Desktop Survival Guide
by Graham Williams |
|||||
Sub-Strings in Tidy |
s <- "string manipulation"
str_sub(s, start=3, end=6)
str_sub(s, 3, 6)
A negative is used to count from the end of the string. |
str_sub(s, 1, -8)
Replacing a sub-string with another string is straightforward using the assignment operator. |
str_sub(s, 1, -8) <- "stip"
s
The function also operates over a vector of strings. |
v <- c("string", "manipulation", "always", "fascinating")
str_sub(v, -4, -1)
str_sub(v, -4, -1) <- "RING"
v
|