我有个人级别的数据,想通过分组动态地总结一个结果。
例如:
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)]
I wanted something like:
dt[ , plot(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
吗?