Reputation: 139
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.
Upvotes: 2
Views: 5413
Reputation: 13319
A dplyr-lubridate
solution:
mydata %>%
mutate(Month = lubridate::month(Month, label = TRUE, abbr = FALSE))
# A tibble: 4 x 4
camp Acct Balance Month
<chr> <dbl> <dbl> <ord>
1 Platinum 2018-03 1 222 January
2 Reboarding 2018 33 7744 April
3 New Acct Auto Jul18 6 949 June
4 Loan2019-4 43 123 August
Upvotes: 1
Reputation: 270298
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
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
Upvotes: 8