Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

r - month language in the as.date function

I don't know how to figure out this problem. That's a piece of my 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")

I convert the Date column into a Date object using:

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

Then I created the Month column from the Date one, typing:

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

But the month names are in Italian and when I try to create another column of ordered factor

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

month names are not recognized and it just appears a column NA. So my question is: is it possible to change the language of the months when creating the new column? Else, is it possible for R to recognize the months as an ordered factor even if the month names are not in English? I need the months as an ordered factor because I have to plot values like explained in this post.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It works for me... there is no italian as you overwrite it with df$Month <- months(as.Date(df$Date)) can you please give us your sessionInfo(), here is mine

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

try to set LC_TIME to C too with Sys.setlocale("LC_TIME", "C");


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...