A simple way to do this with R
base is through the command substr
. With it, I can extract a substring indicating where it begins and where it ends.
Let’s look at two practical examples, with the names Joao and Maria:
Joao
1234
Maria
12345
To extract ao
of Joao, the substr
need to extract characters 3 and 4. To extract ia
of Mary, the command substr
need to extract characters 4 and 5. It is easy to see that both drawings start in the penultimate character (nchar(Joao) - 1
) and end at the last character (nchar(Joao)
). Similarly, (nchar(Maria) - 1
) and (nchar(Maria)
).
Automating this, we have the code below:
x <- c("Joao", "Maria", "Pedro", "Juliana")
substr(x, start = nchar(x)-1, stop = nchar(x))
#> [1] "ao" "ia" "ro" "na"
Created on 2020-07-10 by the reprex package (v0.3.0)
Thank you very much!
– Theorp
It’s great to know that my response has helped you in some way. So consider vote and accept the answer, so that in the future other people who experience the same problem have a reference to solve it.
– Marcus Nunes
Ready! Thanks for the warning! :)
– Theorp