私は時系列解析には新しくxts(と一般にR)を使用していますので、質問の基本的な性質を許してください。xtsオブジェクトのデータを時間と第2の変数(因子)で集計する
私は、データを時間枠(例:月)と第2因子変数で集計したいと考えています。
colour value
2015-01-30 "Blue" "2"
2015-03-15 "Blue" "9"
2015-03-22 "Blue" "9"
2015-08-13 "Blue" "5"
2015-09-01 "Blue" "8"
2015-11-10 "Red" "7"
2016-04-26 "Blue" "2"
2016-07-06 "Red" "9"
2016-07-07 "Red" "6"
2016-07-08 "Red" "2"
2016-10-01 "Red" "6"
2016-11-07 "Red" "2"
は、私が使用して「値」変数をまとめることができます:
apply.monthly(df$value, FUN = mean)
require(xts)
# Create example df and convert it to an xts object
date <- sample(seq(as.Date("2015/01/01"), as.Date("2016/12/31"), by="day"),12)
colour <- c("Red", "Red", "Blue", "Blue", "Blue", "Blue", "Red", "Red", "Red",
"Red", "Blue", "Blue")
value <- sample(1:10, 12, replace = TRUE)
df <- cbind.data.frame(date, colour, value)
df <- xts(df[,-1], order.by = df$date)
これは次のようになりますサンプルデータフレームを作成します。私の質問を説明するために、以下を参照してください
私に与える:
value
2015-01-30 2.000000
2015-03-22 9.000000
2015-08-13 5.000000
2015-09-01 8.000000
2015-11-10 7.000000
2016-04-26 2.000000
2016-07-08 5.666667
2016-10-01 6.000000
2016-11-07 2.000000
しかし、(この場合は)色変数を集計する方法はよくわかりません(月ごとに各色の合計が必要です)。どんな助けでも大歓迎です。
非常に役立つ@NBATrends Thiクラスの「文字」変数である「月」変数を作成します。出力がクラス「日付」のままであることを保証する方法はありますか?私は 'as.Date(df $ Month、format ="%Y-%m ")'を試しましたが、役に立たない。あなたの助けに感謝。 – drgregmartin