2012-12-05 9 views
9

Wenn ich versuche, einen Zeitstempel im folgenden Format zu analysieren: "Do 8. November 15:41:45 2012", wird nur NA zurückgegeben.strptime, as.POSIXct und as.Date kommen unerwartet zurück NA

Ich benutze Mac OS X, R 2.15.2 und Rstudio 0.97.237. Die Sprache meines Betriebssystems ist Niederländisch: Ich nehme an, das hat etwas damit zu tun.

Wenn ich strptime versuchen, NA zurückgegeben:

var <- "Thu Nov 8 15:41:45 2012" 
strptime(var, "%a %b %d %H:%M:%S %Y") 
# [1] NA 

Genauso wenig wie as.POSIXct Arbeit:

as.POSIXct(var, "%a %b %d %H:%M:%S %Y") 
# [1] NA 

Ich habe auch versucht as.Date auf der Saite oben, aber ohne %H:%M:%S Komponenten:

as.Date("Thu Nov 8 2012", "%a %b %d %Y") 
# [1] NA 

Irgendwelche Ideen, was ich falsch machen könnte?

+1

Ich kann Ihren Fehler auf Ubuntu und R Basis nicht reproduzieren. Außerdem erzeugt "strptime" für mich ein "POSIXlt" anstatt eines "POSIXct" Zeitobjekts. Versuchen Sie schließlich, 'as.POSIXct (var, format = ...)' zu verwenden und sehen Sie, ob Sie mehr Glück haben. – Justin

Antwort

17

Ich denke, es ist genau so, wie Sie vermutet haben, strptime fehlschlägt, Ihre Datum-Uhrzeit-Zeichenfolge aufgrund Ihrer Gebietsschemas zu analysieren. Ihre Zeichenfolge enthält sowohl den abgekürzten Wochentag (%a) als auch den abgekürzten Monatsnamen (%b). Diese Zeitangaben sind in ?strptime beschrieben:

Details

%a : Abbreviated weekday name in the current locale on this platform

%b : Abbreviated month name in the current locale on this platform.

"Note that abbreviated names are platform-specific (although the standards specify that in the C locale they must be the first three letters of the capitalized English name:"

"Knowing what the abbreviations are is essential if you wish to use %a , %b or %h as part of an input format: see the examples for how to check."

See also

[...] locales to query or set a locale.

Die Frage der locales relevant ist auch für as.POSIXct, as.POSIXlt und as.Date.

Von ?as.POSIXct:

Details

If format is specified, remember that some of the format specifications are locale-specific, and you may need to set the LC_TIME category appropriately via Sys.setlocale . This most often affects the use of %b , %B (month names) and %p (AM/PM).

Von ?as.Date:

Details

Locale-specific conversions to and from character strings are used where appropriate and available. This affects the names of the days and months.


, also wenn Wochentags- und Monatsnamen in der Kette unterscheiden sich von denen in der aktuellen Locale, strptime, as.POSIXct und as.Date Fehler beim Parsen der Zeichenfolge, und NA wird zurückgegeben.

Allerdings können Sie dieses Problem lösen, indem sie den sich ändernden locales:

# First save your current locale 
loc <- Sys.getlocale("LC_TIME") 

# Set correct locale for the strings to be parsed 
# (in this particular case: English) 
# so that weekdays (e.g "Thu") and abbreviated month (e.g "Nov") are recognized 
Sys.setlocale("LC_TIME", "en_GB.UTF-8") 
# or 
Sys.setlocale("LC_TIME", "C") 

#Then proceed as you intended 
x <- "Thu Nov 8 15:41:45 2012" 
strptime(x, "%a %b %d %H:%M:%S %Y") 
# [1] "2012-11-08 15:41:45" 

# Then set back to your old locale 
Sys.setlocale("LC_TIME", loc) 

Mit meiner persönlichen locale ich Ihre Fehler reproduzieren kann:

Sys.setlocale("LC_TIME", loc) 
# [1] "fr_FR.UTF-8" 

strptime(var,"%a %b %d %H:%M:%S %Y") 
# [1] NA 
0

nur um mit dem gleichen Problem war durcheinander, und gefunden Diese Lösung ist viel sauberer, da keine Systemeinstellungen manuell geändert werden müssen, da im Paket lubridate eine Wrapperfunktion für diesen Job ausgeführt wird und Sie lediglich das Argumentfestlegen müssen:

date <- c("23. juni 2014", "1. november 2014", "8. marts 2014", "16. juni 2014", "12. december 2014", "13. august 2014") 
df$date <- dmy(df$Date, locale = "Danish") 
[1] "2014-06-23" "2014-11-01" "2014-03-08" "2014-06-16" "2014-12-12" "2014-08-13" 
+2

In Bezug auf "es ist nicht notwendig, Systemeinstellungen zu ändern", beachten Sie bitte, dass das 'locale' -Argument in den 'lubridate'-Funktionen nur ein Convenience-Wrapper der in der obigen Antwort beschriebenen Schritte ist: (1) aktuelles Gebietsschema speichern, (2) Gebietsschema ändern, (3) zum ursprünglichen Gebietsschema zurückkehren. Überprüfen Sie den Code [hier] (https://github.com/hadley/lubridate/blob/master/R/parse.r): 'orig_locale <- Sys.getlocale (" LC_TIME "); Sys.setlocale ("LC_TIME", Gebietsschema); on.exit (Sys.setlocale ("LC_TIME", orig_locale)) ' – Henrik