次のデータについて時系列解析を行いたい 時系列データに変換できません。 与えられたリンクからダウンロードすることができますhttps://datamarket.com/data/set/22ox/monthly-milk-production-pounds-per-cow-jan-62-dec-75#!ds=22ox&display=lineデータを時系列データにするためにデータを書式設定する方法
私は分割が、私はとしてエクスポート問題Image of data set
次のデータについて時系列解析を行いたい 時系列データに変換できません。 与えられたリンクからダウンロードすることができますhttps://datamarket.com/data/set/22ox/monthly-milk-production-pounds-per-cow-jan-62-dec-75#!ds=22ox&display=lineデータを時系列データにするためにデータを書式設定する方法
私は分割が、私はとしてエクスポート問題Image of data set
された後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
'as.Date(Month、format ="%Y-%m-01 ")'を実行するだけで、年月フォーマットを扱うこともできます。そうすれば、あなたの目標が時系列であれば、はるかに実用的なクラス「Date」のオブジェクトを得ることができます。 – AkselA
これはずっと良いアイデアです - OPは時系列を求めていたからです。私は答えを更新しました。 –
を私は、データの定期的なCSVバージョンをエクスポートして見ていました。特に厄介なことは何も見えません。何を試したのか、どこが間違っていたのか、私たちに教えてください。 – AkselA