I am new to R programming and have searched SO for many hours. I would appreciate your help.
I have a dataframe, with 3 columns (Date,Description, Debit)
Date Description Debit
2014-01-01 "abcdef VA" 15
2014-01-01 "ghijkl" NY" 56
I am trying to extract the last 2 chars of the second (Description) column (i.e. the 2 letter state abbreviation). I am not very comfortable with apply-type functions.
I have tried using
l <- lapply(a$Description, function(x) {substr(x, nchar(x)-2+1, nchar(x))})
but get the following error message
Error in nchar(x) : invalid multibyte string, element 1
I have tried multiple other approaches, but with the same error.
I am quite sure that I am missing something very basic, so would appreciate your help
thanks
library(stringr)
str_sub(a$Description,-2,-1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With