Summing up values within an interval from another data.frame in R

扶醉桌前 提交于 2019-12-06 15:17:52

Here's one possibility that results in what you described as desired result. I'm not sure whether this is 100% dplyr idiomatic because I'm working on two different data.frames, but anyway, it seems to work.

library(dplyr)

hkdata.2 <- hkdata.2 %>%
  group_by(houseID, member) %>%
  mutate(Totalmxtemp = sum(weather.data$mxtemp[weather.data$date >= date.begin &
                                             weather.data$date <= date.end]))

hkdata.2
#Source: local data frame [2 x 7]
#Groups: houseID, member
#
#  row.names houseID member male   date.end date.begin Totalmxtemp
#1         1       1      1    1 2008-01-07 2008-01-02       118.1
#2         2       1      2    0 2008-01-06 2008-01-04        60.0
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!