我有个人数据,我试图针对这些数据按组动态总结结果。

例子:

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)]

我想要类似的东西:
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可以完成工作吗?

最佳答案

使用matplotdcast的基本 R 解决方案

dt_agg <- dt[ , .(mean = mean(outcome)), by=.(grp,time)]
dt_cast <- dcast(dt_agg, time~grp, value.var="mean")
dt_cast[ , matplot(time, .SD[ , !"time"], type="l", ylab="mean", xlab="")]
# alternative:
dt_cast[ , matplot(time, .SD, type="l", ylab="mean", xlab=""), .SDcols = !"time"]

结果:

关于r - 在data.table中按组绘制,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28400446/

10-12 19:23