as.date函数中的月份语言

时间:2013-08-22 12:31:08

标签: r date

我不知道如何弄清楚这个问题。那是我的df的一部分:

structure(list(ID = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L), .Label = c("B0", "B1", "B12", "B2", "B21", "B22", "B26", 
"B3", "B33", "B4", "B7", "P1", "P21", "P24", "P24 ", "P25", "P27", 
"P28", "P29"), class = "factor"), Date = structure(c(9839, 9946, 
10045, 10133, 10190, 10302, 10354, 10423, 10528, 10676, 10756, 
10841, 10904, 11032, 11129, 11227, 11290, 11390, 11485, 11571, 
11645, 11725, 11843, 11928, 12003, 12128, 12221, 12305, 12380, 
12499, 12549, 12640, 12716, 12856, 12926, 12996, 13104, 13580, 
13671, 13759, 13802), class = "Date"), T = c(9.6, 10.1, 10.4, 
9.9, 9.4, 9.8, 10, 9.8, 9.8, 9.9, 10.3, 10.6, 9.9, 10, 10.3, 
10.1, 10.3, 10, 10.2, 10.4, 10.1, 10.1, 10.1, 10.5, 10.3, NA, 
NA, NA, NA, 10.3, 10.4, 10.9, 10.6, 10.4, 10.7, 10.2, 10, 10.2, 
10.6, 10.5, 10.4), ph = c(6.9, 7.08, 6.96, 7, 7, 6.97, 6.92, 
7.02, 6.93, 6.91, 6.83, 6.87, 6.8, 6.92, 7.02, 6.94, 6.94, 6.86, 
6.9, 6.89, 6.9, 6.97, 6.92, 6.93, 6.91, 6.88, 6.93, 6.78, 6.87, 
6.91, 6.82, 6.91, 6.98, 6.99, 6.79, 6.91, 6.61, 6.86, 6.93, 6.88, 
6.74), EC = c(2810, 3020, 2170, 2511, 1695, 3100, 2510, 1759, 
1101, 3330, 5370, 3300, 3210, 921, 2300, 3380, 3340, 2850, 3430, 
3510, 3450, 3400, 3280, 3170, 3210, 3250, 3010, 2970, 3080, 3120, 
3100, 3040, 3100, 2940, 3050, 3070, 3040, 2270, 2990, 2830, 3010
), O2 = c(0.1, 0.1, 1.3, 0.2, 0.2, 0.1, NA, 0.2, 0.1, 0.2, 0.1, 
NA, NA, NA, 0.1, 0.1, NA, 0.1, 0.1, 0.2, NA, NA, 0.2, 0.1, 0.1, 
0, 0, 0.1, 0.4, 0.2, 0.2, 0.3, 0.2, 0.1, 0.1, 0.3, 0.7, 0.2, 
0.4, 0.2, 0.2), Cl = c(696, 718, 722, 856, 776, 752, 745, 788, 
822, 727, 650, 800, 766, 700, 800, 720, 760, 710, 730, 720, 810, 
610, 720, 830, 820, 740, 670, 510, 710, 500, 640, 630, 650, 430, 
660, 660, 630, 560, 680, 670, 670), SO4 = c(152, 111, 133, 245, 
194, 110, 105, 104, 185, 156, 137, 194, 196, 170, 220, 230, 240, 
200, 220, 200, 220, 170, 230, 210, 240, 280, 240, 190, 260, 360, 
280, 250, 220, 380, 240, 240, 230, 320, 220, 210, 220), NO2 = c(NA, 
NA, NA, NA, NA, NA, NA, 0.0067, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, 0.015, NA, NA, NA, NA, NA, 0.01, 
NA, NA, NA, 0.031, NA, NA, NA, NA, NA, NA, NA), NO3 = c(0.15, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, 0.06, NA, 0.02, NA, 0.02, 0.07, 0.2, 
0.02, NA, NA, NA, 0.05, 0.08, NA, NA, NA, NA, NA), Fe = c(22, 
20, NA, 23, NA, 25, NA, NA, NA, NA, 27, NA, NA, NA, 32, NA, NA, 
NA, 33, NA, NA, NA, 33, NA, NA, NA, 29, NA, NA, NA, 9, NA, NA, 
NA, 8.3, NA, NA, NA, 17, NA, NA), Mn = c(3.8, 3.8, NA, 4.5, NA, 
4.7, NA, NA, NA, NA, 4.9, NA, NA, NA, 5.8, NA, NA, NA, 6, NA, 
NA, NA, 6, NA, NA, NA, 5.3, NA, NA, NA, 4.1, NA, NA, NA, 4.2, 
NA, NA, NA, 4.9, NA, NA), Month = c("dicembre", "marzo", "luglio", 
"settembre", "novembre", "marzo", "maggio", "luglio", "ottobre", 
"marzo", "giugno", "settembre", "novembre", "marzo", "giugno", 
"settembre", "novembre", "marzo", "giugno", "settembre", "novembre", 
"febbraio", "giugno", "agosto", "novembre", "marzo", "giugno", 
"settembre", "novembre", "marzo", "maggio", "agosto", "ottobre", 
"marzo", "maggio", "agosto", "novembre", "marzo", "giugno", "settembre", 
"ottobre")), .Names = c("ID", "Date", "T", "ph", "EC", "O2", 
"Cl", "SO4", "NO2", "NO3", "Fe", "Mn", "Month"), row.names = c(NA, 
-41L), class = "data.frame")

我使用以下命令将Date列转换为Date对象:

df$Date<-as.Date(df$Date, "%d.%m.%y")

然后我从Date 1创建了Month列,输入:

df$Month <- months(as.Date(df$Date))

但月份名称是意大利语,当我尝试创建另一列有序因子

df$Month_factor<-factor(df$Month levels=month.name, ordered=T)

月份名称无法识别,只显示NA列。所以我的问题是:在创建新列时是否可以更改月份的语言?另外,即使月份名称不是英文,R是否有可能将月份识别为有序因子? 我需要将月份作为有序因子,因为我必须绘制this post中解释的值。

1 个答案:

答案 0 :(得分:13)

它适用于我...没有意大利语,因为你用df$Month <- months(as.Date(df$Date))覆盖它可以请你给我们sessionInfo(),这是我的

sessionInfo()

R version 3.0.1 (2013-05-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=French_France.1252  LC_CTYPE=French_France.1252    LC_MONETARY=French_France.1252
[4] LC_NUMERIC=C                   LC_TIME=C                     

attached base packages:
[1] stats     graphics  grDevices datasets  utils     methods   base     

other attached packages:
[1] Rcpp_0.10.4       TeachingDemos_2.9 fastmatch_1.0-4   fasttime_1.0-0    data.table_1.8.9  bit64_0.9-2      
[7] bit_1.1-10        vimcom_0.9-8     

loaded via a namespace (and not attached):
[1] tools_3.0.1

尝试使用LC_TIME

C设置为Sys.setlocale("LC_TIME", "C");