Convert Numeric Values to Month Names & Abbreviations R (2 Examples)

 

In this article, I’ll illustrate how to extract month names and abbreviations based on a numeric month vector in R.

Table of contents:

Let’s get started.

 

Creating Exemplifying Data

The following data is used as basement for this R programming language tutorial:

my_months_num <- c(3, 1, 12, 3, 7)             # Create numeric vector of months
my_months_num                                  # Print numeric vector of months
# [1]  3  1 12  3  7

Have a look at the previously shown output of the RStudio console. It shows that our example data is a vector containing five numeric vector elements. These vector elements represent certain months of the year.

 

Example 1: Convert Numeric Vector to Month Names Using month.name Object

In this example, I’ll explain how to select month names that correspond to our numeric vector by using the month.name object.

The month.name object is a predefined object in the R programming language that contains each of the twelve months in a vector of character strings.

We can subset this vector to convert our numeric vector to a vector of month names as shown below:

my_months_name <- month.name[my_months_num]    # Convert numeric to month names
my_months_name                                 # Print month names
# [1] "March"    "January"  "December" "March"    "July"

Have a look at the RStudio console output: As you can see, we have created a vector of month names with the previous R syntax.

 

Example 2: Convert Numeric Vector to Month Abbreviations Using month.abb Object

The following R programming syntax illustrates how to use the month.abb object to create a vector of month abbreviations.

Similar to the previous example, we can subset the month.abb data object like this:

my_months_abb <- month.abb[my_months_num]      # Convert numeric to month abbreviations
my_months_abb                                  # Print month abbreviations
# [1] "Mar" "Jan" "Dec" "Mar" "Jul"

As you can see, the previous R code has created a vector of month abbreviations.

 

Video & Further Resources

Have a look at the following video instruction of my YouTube channel. In the video, I illustrate the content of this page.

 

 

Furthermore, you might want to read the related tutorials of this website. Some interesting tutorials are listed below.

 

In this article, I have illustrated how to convert a numeric vector to month names and abbreviations in R programming. Let me know in the comments below, if you have any additional questions. Besides that, please subscribe to my email newsletter for regular updates on new articles.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top