我有一张日常温度表
+-------+-----+-------------+
| City | Day | Temperature |
+-------+-----+-------------+
| Miami | 1 | 25 |
| Miami | 2 | 27 |
| Miami | 3 | 34 |
| Miami | 4 | 23 |
| Miami | 5 | 30 |
| Miami | 6 | 31 |
| Paris | 1 | 15 |
| Paris | 2 | 17 |
| Paris | 3 | 14 |
| Paris | 4 | 13 |
| Paris | 5 | 10 |
| Paris | 6 | 11 |
+-------+-----+-------------+
我可以在summarize
天内以n
的方式在城市中+-------+-----+---------------------+
| City | Day | AVGTemperature |
+-------+-----+---------------------+
| Miami | 1-3 | 28.66 |
| Miami | 4-6 | 29 |
| Paris | 1-3 | 15.33 |
| Paris | 4-6 | 14.5 |
+-------+-----+---------------------+
。
结果的例子,大块为3天
AVGTemp <- ddply(temp, .(Day, City), summarize, AVGTemperature=mean(Temperature))
我能做到
n
但这给了我每一天的平均值。我能做到这样它会返回var space[i]= document.getElementById('space[i]');
天的块?
答案 0 :(得分:0)
这是一个dplyr解决方案。将breaks
从3更改为您想要的块数。
library(dplyr)
tab %>%
mutate(day_group = cut(Day, 3, include.lowest = TRUE, labels = FALSE)) %>%
group_by(City, day_group) %>%
summarise(mean_temp = mean(Temperature), start_day = min(Day), end_day = max(Day))
# Source: local data frame [6 x 5]
# Groups: City [?]
#
# City day_group mean_temp start_day end_day
# (fctr) (int) (dbl) (int) (int)
# 1 Miami 1 26.0 1 2
# 2 Miami 2 28.5 3 4
# 3 Miami 3 30.5 5 6
# 4 Paris 1 16.0 1 2
# 5 Paris 2 13.5 3 4
# 6 Paris 3 10.5 5 6