replace many elements in an vector (in R)

Viewed 43
set.seed(12345)
my.months <- sample(1:12, 50, replace = TRUE)

I need to generate a new vector based on "my.months". replace 1 to "Jan",2 to "Feb"...

I try this: replace(my.months, my.months == 3, month.abb[3])

but I don't want to write 12 times, have more efficient way I try?

thanks for any answer =)

1 Answers

Here, the my.months can be used as numeric index - i.e. whereever there are 1, it replace with the first element of 'month.abb' i.e. 'Jan', or 2 with 'Feb' and so on...

month.abb[my.months]
Related