2012-12-05 9 views
9

"Thu Nov 8 15:41:45 2012"のタイムスタンプを解析しようとすると、NAのみが返されます。strptime、as.POSIXct、as.Date return予期しないNA

私はMac OS X、R 2.15.2、Rstudio 0.97.237を使用しています。私のOSの言語はオランダ語です:これはこれと関係があると推測します。どちらも

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

as.POSIXct仕事ん:

私はstrptimeを試し、NAが返されません

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

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

私はまた、上記の文字列ではなく%H:%M:%Sコンポーネントなしas.Dateを試してみました

私は何が間違っている可能性がある任意のアイデア?

+1

私はUbuntuとRベースでエラーを再現できません。また私にとって、 'strptime'は' POSIXct'時間オブジェクトではなく 'POSIXlt'を生成します。最後に、代わりに 'as.POSIXct(var、format = ...)'を使って、もっと運があるかどうか確認してください。 – Justin

答えて

17

あなたのロケールのために、strptimeがあなたの日時文字列を解析できないと思います。あなたの文字列には、平日曜日(%a)と短縮月名(%b)の両方が含まれています。これらの時間仕様は?strptimeで説明されています

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.

localesの問題はas.POSIXctas.POSIXltas.Dateにも関連しています。 ?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).

?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.


このように、文字列中の平日と月の名前は、現在のロケールのもの、strptimeas.POSIXctas.Date異なる場合文字列を正しく解析できず、NAが返されます。

ただし、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) 

私はあなたのエラーを再現することができます

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

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

はちょうど同じ問題をいじり、及び発見されましたこのソリューションは、lubridateパッケージでこのジョブを実行するラッパー関数が存在するため、システム設定を手動で変更する必要がないため、より洗練されたものになります。実行するには、引数locale

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

"システム設定を変更する必要はありません"については、 'lubridate'関数の' locale'引数は、上記の答えに記載されている手順の便利なラッパーにすぎないことに注意してください:(1)現在のロケールを保存し、 (2)ロケールを変更する、(3)元のロケールに戻すコード[here](https://github.com/hadley/lubridate/blob/master/R/parse.r)をチェックしてください: 'orig_locale < - Sys.getlocale(" LC_TIME "); Sys.setlocale( "LC_TIME"、ロケール); on.exit(Sys.setlocale( "LC_TIME"、orig_locale)) ' – Henrik