month name from month number in R

How to get the month name from the number in R

There are multiple scenarios where you would like to get the month name from the number in R. Here are two solutions to get that depending on the month number location. The month number might already be in a separate column or date.

 

In this example, I will use the airquality dataset. It already contains a column with month numbers.

data("airquality")

head(airquality)

#  Ozone Solar.R Wind Temp Month Day
#1    41     190  7.4   67     5   1
#2    36     118  8.0   72     5   2
#3    12     149 12.6   74     5   3
#4    18     313 11.5   62     5   4
#5    NA      NA 14.3   56     5   5
#6    28      NA 14.9   66     5   6

 

Get the month name from the number in R

The easiest way to get the month name from the month number in R is by using built-in constants.

By using month.name you can get full month name in English or using month.abb you can get three-letter abbreviations for the English month names. If you want to get results in a different language, look at the example at the end of this post.

unique(airquality$Month)

#[1] 5 6 7 8 9


month.name[unique(airquality$Month)]

#[1] "May"       "June"      "July"      "August"    "September"


month.abb[unique(airquality$Month)]

#[1] "May" "Jun" "Jul" "Aug" "Sep"

It is important to use square brackets because month.name and month.abb are not functions. Here is how to get the month name from the numeric month in a separate data frame column.

airquality$MonthName <- month.abb[airquality$Month]

head(airquality)

#  Ozone Solar.R Wind Temp Month Day MonthName
#1    41     190  7.4   67     5   1       May
#2    36     118  8.0   72     5   2       May
#3    12     149 12.6   74     5   3       May
#4    18     313 11.5   62     5   4       May
#5    NA      NA 14.3   56     5   5       May
#6    28      NA 14.9   66     5   6       May

Alternatively, you can use the function mutate from the dplyr to get the same result.

require(dplyr)


airquality %>% 
  mutate('MonthName' = month.abb[Month]) %>% 
  head()

#  Ozone Solar.R Wind Temp Month Day MonthName
#1    41     190  7.4   67     5   1       May
#2    36     118  8.0   72     5   2       May
#3    12     149 12.6   74     5   3       May
#4    18     313 11.5   62     5   4       May
#5    NA      NA 14.3   56     5   5       May
#6    28      NA 14.9   66     5   6       May

 

Get the month name from the number in R by extracting it from a date

If the dataset has a column with dates, you can use a format function to get the month name. To get in this data frame a column with dates, I will join separate year, month, and day components.

data("airquality")

require(lubridate)


airquality$Date <- make_date(1973, airquality$Month, airquality$Day)

head(airquality)

#  Ozone Solar.R Wind Temp Month Day       Date
#1    41     190  7.4   67     5   1 1973-05-01
#2    36     118  8.0   72     5   2 1973-05-02
#3    12     149 12.6   74     5   3 1973-05-03
#4    18     313 11.5   62     5   4 1973-05-04
#5    NA      NA 14.3   56     5   5 1973-05-05
#6    28      NA 14.9   66     5   6 1973-05-06

 

Here are examples with the format function that can get the month name from a date.

airquality$MonthName <- format(airquality$Date, "%B")

airquality$MonthNameAbb <- format(airquality$Date, "%b")

tail(airquality)

#    Ozone Solar.R Wind Temp Month Day       Date MonthName MonthNameAbb
#148    14      20 16.6   63     9  25 1973-09-25 September          Sep
#149    30     193  6.9   70     9  26 1973-09-26 September          Sep
#150    NA     145 13.2   77     9  27 1973-09-27 September          Sep
#151    14     191 14.3   75     9  28 1973-09-28 September          Sep
#152    18     131  8.0   76     9  29 1973-09-29 September          Sep
#153    20     223 11.5   68     9  30 1973-09-30 September          Sep

If you want to change your locale in R to get the month name in the desired language, you can do that like this.

Sys.setlocale("LC_TIME", "English")

 

Here is a good cheat sheet that contains date formatting options in R that might be useful in other situations. For example, in the situation when it is necessary to convert text to date in R. Misuse of format code may lead to problems like in this situation.

 

Month name with the lubridate

You can get month names from numeric values using the month function from the lubridate. If your locale is English, then the result is not different from the previous example with R constants.

airquality$LubriMonthName <- lubridate::month(airquality$Date, label = TRUE)

tail(airquality)

#    Ozone Solar.R Wind Temp Month Day       Date MonthName MonthNameAbb LubriMonthName
#148    14      20 16.6   63     9  25 1973-09-25 September          Sep            Sep
#149    30     193  6.9   70     9  26 1973-09-26 September          Sep            Sep
#150    NA     145 13.2   77     9  27 1973-09-27 September          Sep            Sep
#151    14     191 14.3   75     9  28 1973-09-28 September          Sep            Sep
#152    18     131  8.0   76     9  29 1973-09-29 September          Sep            Sep
#153    20     223 11.5   68     9  30 1973-09-30 September          Sep            Sep

The advantage of the lubridate function is an option to specify the locale. For example, if you want to get month names in Latvian, it looks like this.

airquality$MonthNameAbb <- airquality$LubriMonthName <- NULL

airquality$LatvianMonthName <-
  lubridate::month(airquality$Date,
                   label = TRUE,
                   abbr = FALSE,
                   locale = "Latvian")

tail(airquality)

#    Ozone Solar.R Wind Temp Month Day       Date MonthName LatvianMonthName
#148    14      20 16.6   63     9  25 1973-09-25 September       septembris
#149    30     193  6.9   70     9  26 1973-09-26 September       septembris
#150    NA     145 13.2   77     9  27 1973-09-27 September       septembris
#151    14     191 14.3   75     9  28 1973-09-28 September       septembris
#152    18     131  8.0   76     9  29 1973-09-29 September       septembris
#153    20     223 11.5   68     9  30 1973-09-30 September       septembris

 

Here is how to get the weekday name from the date or number in R.


Posted

in

Comments

Leave a Reply

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