2017-03-22 7 views
-3

次のデータについて時系列解析を行いたい 時系列データに変換できません。 与えられたリンクからダウンロードすることができますhttps://datamarket.com/data/set/22ox/monthly-milk-production-pounds-per-cow-jan-62-dec-75#!ds=22ox&display=lineデータを時系列データにするためにデータを書式設定する方法

私は分割が、私はとしてエクスポート問題Image of data set

+1

を私は、データの定期的なCSVバージョンをエクスポートして見ていました。特に厄介なことは何も見えません。何を試したのか、どこが間違っていたのか、私たちに教えてください。 – AkselA

答えて

0

された後str_split_fixed関数は、2つの列にそれをseprateが、時系列として戻って一緒に入れてしようとしてい.TSV(タブ区切り) .csvも同様に動作するはずです。その後data.tableに読んで、今年のように最初の4桁の数字を抽出するためにsubstrを使用(およびintegerに変換)と月のように、最後の2桁:

library(data.table) 
dt <- fread("~/Downloads/monthly-milk-production-pounds-p.tsv") 
dt[, ":="(
    year = as.integer(substr(Month, start = 1, stop = 4)), 
    month = as.integer(substr(Month, start = 6, stop = 7)))] 

>dt 
     Month Monthly milk production: pounds per cow. Jan 62 ? Dec 75 year month 
    1: 1962-01              589 1962  1 
    2: 1962-02              561 1962  2 
    3: 1962-03              640 1962  3 
    4: 1962-04              656 1962  4 
    5: 1962-05              727 1962  5 

AkselAによってコメントに基づいて更新:

へAkselAにより示唆されるように、時系列の使用as.Dateを得る:

library(data.table) 
dt <- fread("~/Downloads/monthly-milk-production-pounds-p.tsv") 
dt[, date_time := as.Date(paste0(Month, "-01"), format="%Y-%m-%d")] 

>dt 
     Month Monthly milk production: pounds per cow. Jan 62 ? Dec 75 date_time 
    1: 1962-01              589 1962-01-01 
    2: 1962-02              561 1962-02-01 
    3: 1962-03              640 1962-03-01 
    4: 1962-04              656 1962-04-01 
    5: 1962-05              727 1962-05-01 
+0

'as.Date(Month、format ="%Y-%m-01 ")'を実行するだけで、年月フォーマットを扱うこともできます。そうすれば、あなたの目標が時系列であれば、はるかに実用的なクラス「Date」のオブジェクトを得ることができます。 – AkselA

+0

これはずっと良いアイデアです - OPは時系列を求めていたからです。私は答えを更新しました。 –

関連する問題