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

爷,独闯天下 提交于 2019-12-08 03:15:21

问题


I have thousands of entries in hkdata.2 and I want to create a loop that can help me to sum up the total exposed mxtemp from another data frame data.1 for each member in each houseID in data.2

Could any expert give me a hand on this?

weather.data
date   mpressure mxtemp     
1   2008-01-01  1025.3  15.7        
2   2008-01-02  1025.6  16.0        
3   2008-01-03  1023.6  18.1        
4   2008-01-04  1021.8  18.4        
5   2008-01-05  1020.1  20.9        
6   2008-01-06  1019.7  20.7        
7   2008-01-07  1018.4  24.0        
8   2008-01-08  1016.7  23.7

hkdata.2
row.names   houseID member  male       date.end date.begin 
1             1       1      1      2008-01-07  2008-01-02      
2             1       2      0      2008-01-06  2008-01-04

I want to get the sum of mxtemp from the date.begin and date.end interval of that same member experienced and show it like this.

hkdata.2
row.names   houseID member          date.end    date.begin  Total.exposed.mxtemp
1             1       1           2008-01-07    2008-01-02     118.1
2             1       2           2008-01-06    2008-01-04     60

total.exposed.mxtemp is the sum of mxtemp within the corresponding interval (which is from date.begin to date.end) ie. In row.names 1, 118.1 = 16+18.1+18.4+20.9+20.7+24

My codes are like this..

> cbind(hkdata.2, t(sapply(apply(hkdata.2, 1, function(x)
+   weather.data[weather.data$date >= x[6] &
+                  weather.data$date <= x[5], c("mxtemp")]), colSums)))

Then I got this error.....:

Error in FUN(X[[1L]], ...) : 
  'x' must be an array of at least two dimensions

Could any expert please help!!


回答1:


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


来源:https://stackoverflow.com/questions/24807775/summing-up-values-within-an-interval-from-another-data-frame-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!