曜日ごとに分割された df/zoo/xts/whatever があります。エントリごとにこれをさらに週ごとに分割したいと思います。
例は金曜日で、各 ID に関連付けられた時間を持つ ID のリストがあります。これらの時間は、1 年間の任意の金曜日である可能性があります。その金曜日に、各 id と各週のカウント (順番) を持つ新しい df を作成したいと思います。
各 w 列が異なる金曜日のカウントである場合、次のようになります。
id w1 w2 w3 w4
1 id_1 1 2 2 8
2 id_2 3 1 5 2
3 id_3 7 4 10 7
出力:
structure(list(id = c("id_1", "id_2", "id_3"), w1 = c(1, 3, 7
), w2 = c(2, 1, 4), w3 = c(2L, 5L, 10L), w4 = c(8L, 2L, 7L)), .Names = c("id",
"w1", "w2", "w3", "w4"), row.names = c(NA, 3L), class = "data.frame")
これは集計の機が熟しているように見えますが、構文を正しく理解できません。私が試した他のことは以下の通りです:
# Applies sum to everything, which doesnt make sense in this context
apply.weekly(friday, sum)
# I considered doing something like getting the unique weeks with:
as.numeric(unique(format(friday[,2], "%U")))
# and then generating each week, getting the counts for each user, and then making a new df from this process. But this seems very inefficient.
編集: str(data[1:20,]) からの出力:
'data.frame': 20 obs. of 2 variables:
$ id : num 1 2 3 4 5 1 2 3 3 2 ...
$ time: POSIXct, format: "2011-04-25 14:00:00" "2011-04-28 20:00:00" "2011-05-03 06:00:00" "2011-05-06 14:00:00" ...
dput(data[1:20,]) からの出力:
structure(list(id = c(1, 2, 3, 4, 5, 1, 2, 3, 3, 2, 1, 4, 3,
2, 1, 4, 3, 2, 1, 7), time = structure(c(1303754400, 1304035200,
1304416800, 1304704800, 1304920800, 1305252000, 1305428400, 1305522000,
1305774000, 1306404000, 1306422000, 1308261600, 1308290400, 1308340800,
1308542400, 1308715200, 1308722400, 1308844800, 1309575600, 1309730400
), class = c("POSIXct", "POSIXt"))), .Names = c("id", "time"), row.names = c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L), class = "data.frame")