Convert month's number to Month name
We can just use month.name
to change the index of months to its corresponding name
month.name[v1]
and there is also a 3 character abbreviation for month
month.abb[v1]
data
set.seed(24)
v1 <- sample(1:12, 24, replace = TRUE)
Convert a month abbreviation to a numeric month, in R
Since month.abb is a system constant, why not use:
match("jan", tolower(month.abb))
# [1] 1
mo2Num <- function(x) match(tolower(x), tolower(month.abb))
mo2Num(c("jan", "JAN", "Feb", "junk") )
#[1] 1 1 2 NA
If you want to see the rest of the relatively small number of "system constants", go to
`?Constants`
The example text implies these should be in the language associated with your locale (although I'm not able to say with authority which of locales that would be. An alternate approach might have been to extract the month numbera after conversion to a POSIXlt-object. This approach requires remembering that the month number os 0-based, so you would need to add 1 in this instance.
How to map month name to month number and vice versa?
Create a reverse dictionary using the calendar
module (which, like any module, you will need to import):
{month: index for index, month in enumerate(calendar.month_abbr) if month}
In Python versions before 2.7, due to dict comprehension syntax not being supported in the language, you would have to do
dict((month, index) for index, month in enumerate(calendar.month_abbr) if month)
How to replace numeric month with a month's full name
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
Replace month abbreviation with numbers
This should do it.
Date = c("12Oct2020","13Oct2020","14Oct2020")
format(strptime(Date, format="%d%b%Y"), format="%d/%m/%Y")
The inner part (strptime()
) converts from your particular date format to a Date
object: the outer part (format()
) prints your date in the requested output format.
Note that this will only work if your computer/R session is using an English locale, because the %b
format specifier looks for the abbreviated month name in the current locale. In French it would still be OK, but in a German locale it would be looking for "Okt" instead ...
What is the easiest or most effective way to convert month's abbreviation to a number in Perl? (ie jan to 1)
borrowed from here
%mon2num = qw(
jan 1 feb 2 mar 3 apr 4 may 5 jun 6
jul 7 aug 8 sep 9 oct 10 nov 11 dec 12
);
and to retrieve
$mon2num{"jan"}
MySQL convert month number to month name
- We can convert the given input number to MySQL date format (focusing on month only), using
Str_To_Date()
function. - Now, we simply need to use
Monthname()
function to extract the month name from the date. - This will work only when
NO_ZERO_DATE
mode is disabled.
Try:
SET sql_mode = ''; -- disable NO_ZERO_DATE mode
SELECT MONTHNAME(STR_TO_DATE(1, '%m'));
As @Felk suggested in comments, if we need to get shortened month name, we can use Date_Format()
function instead:
SET sql_mode = ''; -- disable NO_ZERO_DATE mode
SELECT DATE_FORMAT(STR_TO_DATE(1, '%m'), '%b');
If you don't want to disable the NO_ZERO_DATE
mode, then you can create any random date using the month and call Monthname()
:
SELECT MONTHNAME(CONCAT('2018-',3,'-1')); -- 3 is the input number
Related Topics
Automatically Delete Files/Folders
Code to Import Data from a Stack Overflow Query into R
Non-Equi Join Using Data.Table: Column Missing from the Output
Using Lists Inside Data.Table Columns
How to Update R Packages in Default Library on Windows 7
Perform a Semi-Join with Data.Table
Count Number of Zeros Per Row, and Remove Rows with More Than N Zeros
Converting Two Columns of a Data Frame to a Named Vector
Is Set.Seed Consistent Over Different Versions of R (And Ubuntu)
Dplyr - Using Column Names as Function Arguments
Making a Stacked Bar Plot for Multiple Variables - Ggplot2 in R
How to Remove Empty Factors from Ggplot2 Facets
Finding Point of Intersection in R
Merge and Perfectly Align Histogram and Boxplot Using Ggplot2
Ggplot2: Change Order of Display of a Factor Variable on an Axis
Subsetting Data.Table Using Variables with Same Name as Column