data.table
私は最近私を夢中にさせているこの問題を抱えています。バグのように見えますが、ここで明らかな何かが欠けている可能性があります。
次のデータフレームがあります。
# First some data
data <- data.table(structure(list(
month = structure(c(1356998400, 1356998400, 1356998400,
1359676800, 1354320000, 1359676800, 1359676800, 1356998400, 1356998400,
1354320000, 1354320000, 1354320000, 1359676800, 1359676800, 1359676800,
1356998400, 1359676800, 1359676800, 1356998400, 1359676800, 1359676800,
1359676800, 1359676800, 1354320000, 1354320000), class = c("POSIXct",
"POSIXt"), tzone = "UTC"),
portal = c(TRUE, TRUE, FALSE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE
),
satisfaction = c(10L, 10L, 10L, 9L, 10L, 10L, 9L, 10L, 10L,
9L, 2L, 8L, 10L, 9L, 10L, 10L, 9L, 10L, 10L, 10L, 9L, 10L, 9L,
10L, 10L)),
.Names = c("month", "portal", "satisfaction"),
row.names = c(NA, -25L), class = "data.frame"))
と でまとめたいと思いportal
ますmonth
。古き良きtapply
作品を期待どおりに要約すると、2012 年 12 月と 2013 年 1 月から 2 月の結果を含む 3x2 マトリックスが得られます。
> tapply(data$satisfaction, list(data$month, data$portal), mean)
FALSE TRUE
2012-12-01 8.5 8.000000
2013-01-01 10.0 10.000000
2013-02-01 9.0 9.545455
by
の引数を使用した要約は、次のことをdata.table
行いません。
> data[, mean(satisfaction), by = 'month,portal']
month portal V1
1: 2013-01-01 FALSE 10.000000
2: 2013-02-01 TRUE 9.000000
3: 2013-01-01 TRUE 10.000000
4: 2012-12-01 FALSE 8.500000
5: 2012-12-01 TRUE 7.333333
6: 2013-02-01 TRUE 9.666667
7: 2013-02-01 FALSE 9.000000
8: 2012-12-01 TRUE 10.000000
ご覧のとおり、期待どおりの6 個ではなく、 8 個の値を持つデータ テーブルが返されます。たとえば、とが重複している値。portal == TRUE
month == 2012-02-01
興味深いことに、これを 2013 年のデータだけに制限すると、すべてが期待どおりに機能します。
> data[month >= ymd(20130101), mean(satisfaction), by = 'month,portal']
month portal V1
1: 2013-01-01 TRUE 10.000000
2: 2013-01-01 FALSE 10.000000
3: 2013-02-01 TRUE 9.545455
4: 2013-02-01 FALSE 9.000000
私は信じられないほど困惑しています:)。誰か助けてくれませんか?