将数字月份转换为月份缩写

时间:2014-02-27 03:24:54

标签: r date

我有一个名为month的列作为数字向量的csv。

有没有办法将其转换为月份的缩写月份名称?

5 个答案:

答案 0 :(得分:67)

看一下month.abb常数。例如,假设您有一个由月份数组成的整数向量,那么您可以使用它来获取月份名称的三个字母缩写:

> month <- c(12,3,6,2,3,7)
> month.abb[month]
[1] "Dec" "Mar" "Jun" "Feb" "Mar" "Jul"

答案 1 :(得分:9)

如果您需要非标准月份缩写,请创建自己的月份查找向量:

#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

答案 2 :(得分:8)

使用lubridate,从已知的月份日开始构建向量:

测试:对于这些月份数字,假设Jan = 1:

> m = c(1,2,6,7,9,10,11,12,0,99,NA)

做的:

> 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   

(m-1)所在的位置是因为我们是从1月份开始的。

看看如何比较:

> 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   

请注意,它将月份数字解释为mod-12,因此99映射到3(99 = 3 +(8 * 12)),NA返回NA。已发布的一些答案不会这样做。 -1是11月,因为0是12月。

答案 3 :(得分:4)

如果可以接受英语缩写,则R具有缩写月份名称的内置常量month.abb向量。只需使用数字日期来索引缩写月份名称的向量。例如,使用虚拟数据:

set.seed(1)
df <- data.frame(A = runif(10), Month = sample(12, 10, replace = TRUE))

以下是通过month.abb索引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

答案 4 :(得分:-1)

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