我有個人級別的數據,我試圖根據組動態總結結果。按data.table中的組繪圖
例子:
set.seed(12039)
DT <- data.table(id = rep(1:100, each = 50),
grp = rep(letters[1:4], each = 1250),
time = rep(1:50, 100),
outcome = rnorm(5000))
我想知道繪製組級別摘要最簡單的方式,數據載於:
DT[ , mean(outcome), by = .(grp, time)]
我想是這樣的:
但這根本不起作用。
我上倖存的可行選項(可以很容易地循環)爲:
plot(DT[grp == "a", mean(outcome), by = time])
lines(DT[grp == "b", mean(outcome), by = time])
lines(DT[grp == "c", mean(outcome), by = time])
lines(DT[grp == "d", mean(outcome), by = time])
(與顏色等添加的參數,排除了簡潔)
這令我不做到這一點的最好方法 - 給予data.table
在處理羣體方面的技巧,是不是有更優雅的解決方案?
其他來源已經指向我matplot
,但我不能看到一個簡單的方法來使用它 - 我需要重塑DT
,並有一個簡單的reshape
,將完成這項工作?
這個工作,但'dt_cast [,setdiff(名稱(dt_cast), 「時間」),其中= F]'或'dt_cast [ ,當有多個組時,需要使用等級(dt $ grp),其中= F]'。謝謝! – MichaelChirico 2015-02-09 12:41:17
實際上,最近更新到'data.table'就更容易了! – MichaelChirico 2016-10-05 03:21:08