Consider the folowing data.frame:
df <- structure(list(sufix = c("atizado", "atoria", "atório", "auta",
"áutico", "ável"), min_stem_len = c(4, 5, 3, 5, 4, 2), replacement = c("",
"", "", "", "", ""), exceptions = list(character(0), character(0),
character(0), character(0), character(0), c("afável", "razoável",
"potável", "vulnerável"))), .Names = c("sufix", "min_stem_len",
"replacement", "exceptions"), row.names = 21:26, class = c("tbl_df",
"tbl", "data.frame"))
I have a list of strings in variable sufix
of this data.frame.
Now I have a word word <- "amável"
and I want to get the sufix of this word with the same length as each word of the df$sufix
.
I'm using the folowing code:
library(stringr)
word <- "amável"
str_sub(word, start = -stringr::str_length(df$sufix))
But this outputs this:
> str_sub(word, start = -stringr::str_length(df$sufix))
[1] "amável" "mável" "mável" "vel" "mável" "vel"
> df$sufix
[1] "atizado" "atoria" "atório" "auta" "áutico" "ável"
I was expecting that the last element of the resulting vector to be "ável" since:
> str_length("ável")
[1] 4
> str_sub(word, start = -4)
[1] "ável"
Here a more simple reproducible example:
set.seed(100)
a <- sample(1:10, 10000, replace = T)
res <- rep("ábc", 10000) %>% str_sub(start = -a)
sum(ifelse(a > 3, 3, a) != str_length(res))
[1] 2504