Last characters of a data.frame column in R

Asked

Viewed 89 times

3

I have a table within a date.frame, and I need to get only the last two characters of that table, how do I do that?

bs.: I was trying to do it using str_sub, but in it I can only define which character starts and which ends, and my data varies the character size. Follow my example below that does not solve:

base$estado <- str_sub(psd_base$itbc_name, start = 2)

1 answer

6


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)

  • 1

    Thank you very much!

  • 1

    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.

  • 1

    Ready! Thanks for the warning! :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.