I have the following data frame:
df <- data.frame(string=c('abcde', 'cde'))
I want to find the end position of "de" in each string, which I can determine like so:
df %>%
rowwise() %>%
mutate(pos=str_locate(string = string, pattern = "de")[2])
## string pos
## abcde 5
## cde 3
This is the intended output but I don't wish to use rowwise() because it is very slow for large data frames.
So I tried to vectorize my function and remove the rowwise() command:
Vstr_locate <- Vectorize(str_locate)
df %>%
#rowwise() %>%
mutate(pos=Vstr_locate(string = string, pattern = "de")[2])
But that didn't work:
## string pos
## abcde 5
## cde 5
Questions:
- Why doesn't my vectorized function work?
- How I can get the intended output without using
rowwise()?