我有几列数据。我想按用户找到每个时间段的两个不同值的平均值。然后我想相应地绘制图表。对于每个时间段,每个用户的 metricA 和 statusB 应该只有一个值。我认为它正在工作,但是当我绘制图表时,我在某些用户的同一时间部分看到两条单独的状态 B 行(这不应该发生)。[这与我当前的问题无关,但我正在循环执行此操作。]
我为 metricA 和 statusB 生成平均值。
tempmetricAagg <- aggregate(metricA ~ username + time_chunk, data = tempdf, FUN = mean)
tempstatusBagg <- aggregate(statusB ~ username + time_chunk, data = tempdf, FUN = mean)
然后合并它们(我最初这样做时没有指定列名,但结果是一样的)-
tempmetricAstatusBagg <- merge(tempmetricA, tempstatusB, by =c("username","time_chunk"))
然后我用 ggplot 绘制结果:
ggplot(data=tempmetricAstatusBagg, aes(as.factor(time_chunk), metricA, group=statusB, color = statusB)) + geom_line() + facet_wrap(~ username) + scale_colour_gradient(limits=c(0, 1), low="red")
仔细观察,我相信问题可能出在我如何调用(或未能调用)嵌套元素上。
partfillbygroup <- split(partfill, partfill$group)
for (i in 1:length(partfillbygroup)){
cat(names(partfillbygroup[i]), "\n")
tempdf <- subset(partfillbygroup[[i]][,c("username","metricA", "statusB")])
我也试过:
tempdf <- as.data.frame(partfillbygroup[[i]])
这是来自 dput 的数据:
structure(list(username = structure(c(44L, 44L, 44L, 44L, 44L,
44L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 46L, 46L, 46L, 46L, 46L,
46L, 46L, 46L, 46L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 48L,
48L, 48L, 48L, 48L, 48L, 48L, 48L, 49L, 49L, 49L, 49L, 49L, 49L,
50L, 50L), .Label = c("group21", "group216", "group218", "group219",
"group22", "group220", "group225", "group227", "group228", "group23",
"group230", "group26", "group28", "group29", "group11", "group110",
"group111", "group112", "group113", "group114", "group115",
"group116", "group117", "group118", "group119", "group12",
"group120", "group121", "group122", "group13", "group130",
"group14", "group17", "group18", "group19", "sampleuser1", "sampleuser11",
"sampleuser129", "sampleuser13", "sampleuser130", "sampleuser14", "sampleuser15", "sampleuser16",
"sampleuser17", "sampleuser18", "sampleuser19", "sampleuser20", "sampleuser21", "sampleuser24", "sampleuser26",
"sampleuser30", "sampleuser31", "sampleuser32", "sampleuser33", "sampleuser34", "sampleuser36", "sampleuser37",
"sampleuser38", "sampleuser39", "sampleuser41", "sampleuser42", "sampleuser44", "sampleuser45", "sampleuser46",
"sampleuser47", "sampleuser49", "sampleuser5", "sampleuser50", "sampleuser51", "sampleuser52", "sampleuser53",
"sampleuser54", "sampleuser55", "sampleuser58", "sampleuser59", "sampleuser6", "sampleuser61", "sampleuser63",
"sampleuser64", "sampleuser65", "sampleuser66", "sampleuser67", "sampleuser68", "sampleuser69", "sampleuser72",
"sampleuser73", "sampleuser74", "sampleuser75", "sampleuser76", "sampleuser77", "sampleuser78", "sampleuser79",
"sampleuser8", "sampleuser80", "sampleuser9"), class = "factor"), time_chunk = c(2,
3, 4, 5, 6, 7, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9,
1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5,
6, 6, 7), statusB = c(0, 0, 0, 0.958333333333333, 1, 1, 0,
0, 0, 0.851851851851852, 1, 0.8125, 1, 0, 0, 0, 0.290322580645161,
1, 1, 1, 1, 1, 0, 0, 0, 0, 0.6, 1, 1, 1, 0, 0, 0, 0, 0.727272727272727,
1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0), metricA = c(0.369215384615385,
0.607138888888889, 0.527866666666667, 0.115908333333333, 0.131221739130435,
0.0860222222222222, 0.0370333333333333, 0.0946363636363636, 0.107113043478261,
0.406085185185185, 0.460740909090909, 0.42078125, 0.6807, 0.170962162162162,
0.194261290322581, 0.486108333333333, 0.22921935483871, 0.160673684210526,
0.1475625, 0.272055555555556, 0.31885625, 0.4423, 0.192307692307692,
0.1892, 0.0951933333333333, 0.12151, 0.15072, 0.226752631578947,
0.234642857142857, 0.3227, 0.0992, 0.191246153846154, 0.0694444444444444,
0.0899, 0.129172727272727, 0.144986363636364, 0.290582352941176,
0.351575, 0.153927777777778, 0.143108823529412, 0.178781818181818,
0.12222, 0.114009090909091, 0.414692857142857, 0.269341666666667,
0.361045)), .Names = c("username", "time_chunk", "statusB",
"metricA"), row.names = c(NA, -46L), class = "data.frame")