【发布时间】:2016-09-13 05:11:54
【问题描述】:
我想计算 ds$date_fixed 和 ds$date_broken 之间时间段的平均燃烧时间 (burningshours$hours_burned)。我知道我可以使用下面的代码来计算:
ds$average_burninghours <- sapply (interval(ds$date_fixed, ds$date_broken), function(i)
mean (burning_hours$hours_burned[burning_hours$date%within%i]))
但我想根据位置和位置计算燃烧小时数。 所以,我想添加一些看起来像 lke: 'group_by = c(location, position)' 的代码,但我无法做到这一点。有人对此有想法吗?
示例代码:
ds <- data.frame( date_fixed= c("16-3-2015", "19-3-2015", "21-3-2015"),
date_broken = c("18-3-2015", "22-3-2015", "24-3-2015"),
location = c("A", "B", "B"), position = c("1", "2", "2"))
burning_hours <- data.frame(date = c("16-3-2015", "16-3-2015", "17-3-2015", "17-3-2015",
"18-3-2015", "18-3-2015", "19-3-2015", "19-3-2015", "20-3-2015",
"20-3-2015", "21-3-2015", "21-3-2015", "22-3-2015", "22-3-2015",
"23-3-2015", "23-3-2015", "24-3-2015", "24-3-2015"),
hours_burned= c("10", "11"), location = c("A", "B"),
position = c("1", "2"))
期望的结果:
date_fixed date_broken location position avg_burninghours
16-3-2015 18-3-2015 A 1 10
19-3-2015 22-3-2015 B 2 11
21-3-2015 24-3-2015 B 2 11
【问题讨论】: