我很困惑,爲什麼我無法通過data.table中的mean()獲得每行的平均值。爲什麼不在data.table中首先聚合行
> aaa <- data.table(matrix(1:9, nrow = 3))
> aaa[, `:=` (avg = mean(V1 + V2 +V3), onethird = (V1 + V2 +V3)/3)]
> aaa
V1 V2 V3 avg onethird
1: 1 4 7 15 4
2: 2 5 8 15 5
3: 3 6 9 15 6
似乎什麼data.table所做的是平均值(V1)+平均(V2)+平均(V3),而不是平均值(V1 + V2 + V3)。
~~~~~~~~~~~~~~~~~~~~~~~~~
其實我想通過計算其他列的平均值生成一些更多的列,比如從V1和V2獲取avg12,從V3,V4和V5獲得avg345。
> aaa <- data.table(matrix(1:10, nrow = 2))
> aaa[, `:=` (avg12 = (V1 + V2)/2, avg345 = (V3 + V4 + V5)/3)]
> aaa
V1 V2 V3 V4 V5 avg12 avg345
1: 1 3 5 7 9 2 7
2: 2 4 6 8 10 3 8
是否可以在(V1 + V2)或(V1,V2)上使用一些簡單的均值函數?
'AAA [,V1 + V2 + V3]的數目除以''給出C(12,15,18)' - 的平均這是'15' – thelatemail
謝謝@thelatemail。現在我意識到mean()僅適用於單列。 –
@DavidArenburg - '爲什麼mean()不會在data.table中的行之間聚合?'也許? – thelatemail