diorzyon
diorzyon

Reputation: 21

Is there a function to change my months column from int to text without it showing NA

I have a df month column like this:

"09" "09" "04" "01" "04" "10"

And I am trying to convert them to "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"

I have tried this

df$months <- month.abb[df$month]

and mymonths <- c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec") df$months <- mymonths[ df$month ]

but my output from the both is:

NA NA NA NA NA NA

Upvotes: 0

Views: 48

Answers (1)

akrun
akrun

Reputation: 887048

We need to convert to integer so that it can be used as position index to return the corresponding values of month.abb in that position

month.abb[as.integer(df$month)]

Upvotes: 2

Related Questions