As.Date gives NA for the month name "März" (march)

I have a scraper character vector with dates. My problem: when using as.Date()every date containing the month name "März" (= which means "march" in German) NAed. Why is this?

Here is an example (hopefully reproducible):

require(RCurl)
require(XML)
doc <- htmlParse(getURL("http://www.amazon.de/product-reviews/3836218984/?ie=UTF8&pageNumber=5&showViewpoints=0&sortBy=byRankDescending"), 
                 encoding="UTF-8")
(dates <- xpathSApply(doc, "//div/span[2]/nobr", xmlValue))
# [1] "12. Februar 2009"   "12. November 2006"  "19. März 2010"     
# [4] "30. Juni 2007"      "7. März 2006"       "19. März 2007"     
# [7] "22. Januar 2006"    "24. September 2005" "15. Februar 2012"  
# [10] "28. März 2007" 

Sys.setlocale("LC_TIME", "German") # on Windows, see ?Sys.setlocale
as.Date(dates,  "%d. %B %Y")
# [1] "2009-02-12" "2006-11-12" NA           "2007-06-30" NA          
# [6] NA           "2006-01-22" "2005-09-24" "2012-02-15" NA 

Any ideas on what to do next?

Note that if I apply the same to dputed and copy / ined character, everything is fine:

dates <- c("12. Februar 2009", "12. November 2006", "19. März 2010", "30. Juni 2007", 
           "7. März 2006", "19. März 2007", "22. Januar 2006", "24. September 2005", 
           "15. Februar 2012", "28. März 2007")
as.Date(dates,  "%d. %B %Y")
# [1] "2009-02-12" "2006-11-12" "2010-03-19" "2007-06-30"
# [5] "2006-03-07" "2007-03-19" "2006-01-22" "2005-09-24"
# [9] "2012-02-15" "2007-03-28"

For completeness of my session information:

R version 3.0.2 (2013-09-25)
Platform: x86_64-w64-mingw32/x64 (64-bit)

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

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

loaded via a namespace (and not attached):
[1] tools_3.0.2
+3
source share
3 answers

Windows 7 x64. , R Windows , . latin1 Date .

as.Date(iconv(dates,from='UTF-8',to='latin1'),'%d. %B %Y')
#  [1] "2009-02-12" "2006-11-12" "2010-03-19" "2007-06-30" "2006-03-07" "2007-03-19"
#  [7] "2006-01-22" "2005-09-24" "2012-02-15" "2007-03-28"

, as.Date Windows, .

+5

. , , .

 Sys.setlocale("LC_TIME")

[1] "Italian_Italy.1252"

:

levels(dates)

[1] "1. 2012" "11. 2012" "19. 2012" "20. 2013" "28. 2012 " [6] "7. 2012"

NA , ( )

 head(as.Date(dates, format= "%d. %b. %Y"))

[1] NA NA NA NA NA NA

 summary(GEM_variability$date)

    Min.      1st Qu.       Median         Mean      3rd Qu.         Max. 
"2013-03-20" "2013-03-20" "2013-03-20" "2013-03-20" "2013-03-20" "2013-03-20"              "559"

: strftime

lct <- Sys.getlocale("LC_TIME"); Sys.setlocale("LC_TIME", "C")
dates<- as.Date(date, format="%d. %b. %Y")
#dates<- strptime(date, format="%d. %b. %Y")
Sys.setlocale("LC_TIME", lct)
+2

/.

.

,

months <- c("JAN", "FEB", "MAR", "APR", "MAY", "JUN", 
            "JUL", "AUG", "SEP", "OCT", "NOV", "DEC")
for (month in months) print(
     as.Date(iconv(paste("01", month, "2014", sep=""), 
                  from='UTF-8', to='latin1'), "%d%b%Y"))

[1] "2014-01-01"
[1] "2014-02-01"
[1] NA
[1] "2014-04-01"
[1] NA
[1] "2014-06-01"
[1] "2014-07-01"
[1] "2014-08-01"
[1] "2014-09-01"
[1] NA
[1] "2014-11-01"
[1] "2014-12-01"

, , ( iconv() ).

:

Sys.setlocale("LC_TIME", "en_US.UTF-8")

(iconv() ).

+1

All Articles