Reputation: 921
I have a csv with a column called month as a numeric vector.
Is there any way to convert this to a abbreviated month name of the month?
Upvotes: 72
Views: 138630
Reputation: 29
A<-array(month.name)
A
[1] "January" "February" "March" "April"
[5] "May" "June" "July" "August"
[9] "September" "October" "November" "December"
U<-list(month=A, num=1:12 )
U
$`month`
[,1] [,2] [,3] [,4]
[1,] "January" "April" "July" "October"
[2,] "February" "May" "August" "November"
[3,] "March" "June" "September" "December"
$num
[1] 1 2 3 4 5 6 7 8 9 10 11 12
DF<-data.frame(U$month, U$num)
DF
X1 X2 X3 X4 U.num
1 January April July October 1
2 February May August November 2
3 March June September December 3
4 January April July October 4
5 February May August November 5
6 March June September December 6
7 January April July October 7
8 February May August November 8
9 March June September December 9
10 January April July October 10
11 February May August November 11
12 March June September December 12
Upvotes: 1
Reputation: 56004
If you need non-standard month abbreviation, then create your own month lookup vector:
#dummy data
df <- data.frame(month = c(1,3,5))
#months vector assuming 1st month is Jan.
mymonths <- c("Jan","Feb","Mar",
"Apr","May","Jun",
"Jul","Aug","Sep",
"Oct","Nov","Dec")
#add abbreviated month name
df$MonthAbb <- mymonths[ df$month ]
#result
df
# month MonthAbb
# 1 1 Jan
# 2 3 Mar
# 3 5 May
Upvotes: 21
Reputation: 94172
Use lubridate, construct a vector starting from a known month day:
Test: for these month numbers, assume Jan=1:
> m = c(1,2,6,7,9,10,11,12,0,99,NA)
do:
> require(lubridate)
> as.character(month(ymd(010101) + months(m-1),label=TRUE,abbr=TRUE))
[1] "Jan" "Feb" "Jun" "Jul" "Sep" "Oct" "Nov" "Dec" "Dec" "Mar" NA
where the (m-1)
is because we are starting from a date in January.
To see how that compares:
> cbind(m,as.character(month(ymd(010101) + months(m-1),label=TRUE,abbr=TRUE)))
m
[1,] "1" "Jan"
[2,] "2" "Feb"
[3,] "6" "Jun"
[4,] "7" "Jul"
[5,] "9" "Sep"
[6,] "10" "Oct"
[7,] "11" "Nov"
[8,] "12" "Dec"
[9,] "0" "Dec"
[10,] "99" "Mar"
[11,] NA NA
Note it interprets month numbers as mod-12 so 99 maps to 3 (99=3+(8*12)) and NA returns NA. Some of the answers already posted won't do this. -1 is Nov since 0 is Dec.
Upvotes: 14
Reputation: 61154
Take a look at the month.abb
constant. For example, assume you have a vector of integers consisting of the number of the month, then you can use it to get the three letters abbreviation of the month name by doing:
> month <- c(12,3,6,2,3,7)
> month.abb[month]
[1] "Dec" "Mar" "Jun" "Feb" "Mar" "Jul"
Upvotes: 129
Reputation: 174788
If English language abbreviations are acceptable, R has a built in constant month.abb
vector of the abbreviated month names. Just use your numeric date to index that vector of abbreviate month names. For example, using dummy data:
set.seed(1)
df <- data.frame(A = runif(10), Month = sample(12, 10, replace = TRUE))
here are several options to index month.abb
via Month
:
> with(df, month.abb[Month])
[1] "Mar" "Mar" "Sep" "May" "Oct" "Jun" "Sep" "Dec" "May" "Oct"
> df <- transform(df, MonthAbb = month.abb[Month])
> df
A Month MonthAbb
1 0.26550866 3 Mar
2 0.37212390 3 Mar
3 0.57285336 9 Sep
4 0.90820779 5 May
5 0.20168193 10 Oct
6 0.89838968 6 Jun
7 0.94467527 9 Sep
8 0.66079779 12 Dec
9 0.62911404 5 May
10 0.06178627 10 Oct
Upvotes: 7