string <- "this is a funny cat"
I want to replace the first 15 characters of string
with 'orange`. The desired output is
'orange cat'
However, using substr
gives me
substr(string, 1, 15) <- "orange"
> string
[1] "oranges a funny cat"
which is not the desired output.
The output of substr
should be the pattern
of sub
.
string <- "this is a funny cat"
sub(substr(string, 1, 15), "orange", string)
[1] "orange cat"
Or directly replace the first 15 characters in sub
.
sub("^.{15}", "orange", string)
[1] "orange cat"