Search code examples
rdplyrsubstr

extracting last n characters from a character column in r


I have a data.frame (let's say x) with a character column. I am trying to extract last n (let's say 3) characters from that column and create a new column in the same data.frame. I am trying to do this:

library(dplyr)
x <- x %>% mutate(new_col=substr(old_col, nchar(old_col)-3+1, nchar(old_col)))

The error message:

Error in mutate_impl(.data, dots) : Evaluation error: 'nchar()' requires a character vector.

I also tried this:

x <- x %>% mutate(new_col=substr(x$old_col, nchar(x$old_col)-3+1, nchar(x$old_col)))

Solution

  • Use as.character inside nchar or use stringsAsFactors = F when creating the data.frame.

    library(magrittr)
    df <- data.frame(A = c("Blue", "Orange", "Black"), stringsAsFactors = F)
    df %<>% mutate(B = substr(A, nchar(A)-3+1, nchar(A)))
    df
    
           A   B
    1   Blue lue
    2 Orange nge
    3  Black ack