Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace numeric month with a month's full name

Tags:

r

tibble

Change a column with month in number to the actual month name in full using tidyverse package. Please, bear in mind that even though this data has only four months here, my real dataset contains all actual month of the year.

I am new to tidyverse

mydata <- tibble(camp = c("Platinum 2018-03","Reboarding 2018","New Acct Auto Jul18", "Loan2019-4"),
              Acct = c(1, 33, 6, 43),
              Balance = c(222, 7744, 949, 123),
              Month = c(1,4,6,8))

I expect the output to be January, April, June, August etc. Thanks for your help.

like image 354
Omomaxi Avatar asked Dec 14 '25 18:12

Omomaxi


1 Answers

R comes with a month.name vector which should be ok as long as you only need English names.

mydata %>% mutate(MonthName = month.name[Month])

giving:

# A tibble: 4 x 5
  camp                 Acct Balance Month MonthName
  <chr>               <dbl>   <dbl> <dbl> <chr>    
1 Platinum 2018-03        1     222     1 January  
2 Reboarding 2018        33    7744     4 April    
3 New Acct Auto Jul18     6     949     6 June     
4 Loan2019-4             43     123     8 August 

Other Languages

If you need other languages use this code (or omit as.character to get ordered factor output):

library(lubridate)
Sys.setlocale(locale = "French")
mydata %>% mutate(MonthName = as.character(month(Month, label = TRUE, abbr = FALSE)))

giving:

# A tibble: 4 x 5
  camp                 Acct Balance Month MonthName
  <chr>               <dbl>   <dbl> <dbl> <chr>    
1 Platinum 2018-03        1     222     1 janvier  
2 Reboarding 2018        33    7744     4 avril    
3 New Acct Auto Jul18     6     949     6 juin     
4 Loan2019-4             43     123     8 août  
like image 188
G. Grothendieck Avatar answered Dec 17 '25 12:12

G. Grothendieck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!