substr(x, start, stop) substr(x, start, stop) <- value substring(text, first, last = 1000000L) substring(text, first, last = 1000000L) <- value
| x | a vector of character strings. |
| start | a numeric vector giving the position within each element of x to be the first character in the respective substring. |
| stop | a numeric vector giving the position within each element of x to be the last character in the respective substring. |
| text | the same as x. |
| first | the same as start. |
| last | the same as stop. |
| value | the replacement text. |
substr(c("Illinois", "Indiana", "Iowa"), 1, 5)
# [1] "Illin" "India" "Iowa"
substr(c("Illinois", "Indiana", "Iowa"), 1, c(1,2,3))
# [1] "I" "In" "Iow"
# substr doesn't replicate input string
substr("xxxxxxxxxx", 1, 1:4)
# [1] "x"
# substring replicates input string to match other arguments
substring("xxxxxxxxxx", 1, 1:4)
# [1] "x" "xx" "xxx" "xxxx"
substring("abcde", 1:5, 1:5)
# [1] "a" "b" "c" "d" "e"
substring("This is a test", 6) # start from 6th char
# [1] "is a test"
x <- c("abcdef", "ghijklmnop")
substring(x, 3, 4) <- c("A","BCDF")
x
# [1] "abAdef" "ghBCklmnop"
# substitute one character in first string
# substitute two characters in second string