1 Goal


The goal of this tutorial is to handle month abbreviations in Datasets. We will learn how to transform numerical months into abbreviations and the other way around


2 Data creation


# We are going to create a random vector from 1 to 12 with repetitions to transform it into abbreviations

my_months <- sample(1:12,50, replace = TRUE)
my_months
##  [1] 10  7 12  9 12 10  7  6  9  5  2  5  5  4  3  8 11  6  9  8  4  2  8
## [24]  9  7  7 10 11  7 11  9  4  8 10 10  1  9  3  2  4  4  6  7  1  9  3
## [47] 11  2  3 12

3 Changing numerical months to abbreviations


# There is in R a variable called month.abb
month.abb
##  [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov"
## [12] "Dec"
# We change the number to abbreviations using this variable  
my_months_abb <- month.abb[my_months]
my_months_abb
##  [1] "Oct" "Jul" "Dec" "Sep" "Dec" "Oct" "Jul" "Jun" "Sep" "May" "Feb"
## [12] "May" "May" "Apr" "Mar" "Aug" "Nov" "Jun" "Sep" "Aug" "Apr" "Feb"
## [23] "Aug" "Sep" "Jul" "Jul" "Oct" "Nov" "Jul" "Nov" "Sep" "Apr" "Aug"
## [34] "Oct" "Oct" "Jan" "Sep" "Mar" "Feb" "Apr" "Apr" "Jun" "Jul" "Jan"
## [45] "Sep" "Mar" "Nov" "Feb" "Mar" "Dec"

4 Changing abbreviations to numerical month values


# To change from abbreviations to month number we need to match with the month.abb variable
my_months_number <- match(my_months_abb,month.abb)
my_months_number
##  [1] 10  7 12  9 12 10  7  6  9  5  2  5  5  4  3  8 11  6  9  8  4  2  8
## [24]  9  7  7 10 11  7 11  9  4  8 10 10  1  9  3  2  4  4  6  7  1  9  3
## [47] 11  2  3 12

5 Conclusion


In this tutorial we have learnt how to change from abbreviations of the months of the year to number and the other way around, as easy as using the month.abb variable included in the base of R.