r - 在 R 中使用一个数据帧对另一个数据帧中的一系列数据求和

标签 r dataframe

我正在从 SAS 迁移到 R。我需要帮助弄清楚如何汇总日期范围内的天气数据。在 SAS 中,我采用日期范围,使用数据步骤为每个日期(使用 startdateenddatedate)创建记录范围,与天气合并,然后汇总 (VAR hdd cdd; CLASS=startdate enddate sum=) 以总结日期范围的值。

R代码:

startdate <- c(100,103,107)
enddate <- c(105,104,110)
billperiods <-data.frame(startdate,enddate);

获取:

> billperiods
startdate enddate
1       100     105
2       103     104
3       107     110

R代码:

weatherdate <- c(100:103,105:110)
hdd <- c(0,0,4,5,0,0,3,1,9,0)
cdd <- c(4,1,0,0,5,6,0,0,0,10)
weather <- data.frame(weatherdate,hdd,cdd)

获取:

> weather
   weatherdate hdd cdd
1          100   0   4
2          101   0   1
3          102   4   0
4          103   5   0
5          105   0   5
6          106   0   6
7          107   3   0
8          108   1   0
9          109   9   0
10         110   0  10

注意:weatherdate = 104 缺失。我可能有一天没有天气。

我不知道如何到达:

> billweather
  startdate enddate sumhdd sumcdd
1       100     105      9     10
2       103     104      5      0
3       107     110     13     10

其中 sumhdd 是天气中从 startdateenddatehdd 的总和 数据.frame.

有什么想法吗?

最佳答案

这是使用IRangesdata.table的方法。看来,对于这个问题,这个答案似乎有点矫枉过正。但总的来说,我发现使用 IRanges 来处理间隔很方便,多么简单。

# load packages
require(IRanges)
require(data.table)

# convert data.frames to data.tables
dt1 <- data.table(billperiods)
dt2 <- data.table(weather)

# construct Ranges to get overlaps
ir1 <- IRanges(dt1$startdate, dt1$enddate)
ir2 <- IRanges(dt2$weatherdate, width=1) # start = end

# find Overlaps
olaps <- findOverlaps(ir1, ir2)

# Hits of length 10
# queryLength: 3
# subjectLength: 10
#    queryHits subjectHits 
#     <integer>   <integer> 
#  1          1           1 
#  2          1           2 
#  3          1           3 
#  4          1           4 
#  5          1           5 
#  6          2           4 
#  7          3           7 
#  8          3           8 
#  9          3           9 
#  10         3          10 

# get billweather (final output)
billweather <- cbind(dt1[queryHits(olaps)], 
                dt2[subjectHits(olaps), 
                list(hdd, cdd)])[, list(sumhdd = sum(hdd), 
                sumcdd = sum(cdd)), 
                by=list(startdate, enddate)]

#    startdate enddate sumhdd sumcdd
# 1:       100     105      9     10
# 2:       103     104      5      0
# 3:       107     110     13     10

最后一行的代码分割:首先,我使用 queryHitssubjectHitscbind 构建中间路径然后,我从 data.table 中按 startdate、enddate 进行分组,并获取 hdd 的总和和 cdd 的总和>。单独查看该行会更容易,如下所示,以便更好地理解。

# split for easier understanding
billweather <- cbind(dt1[queryHits(olaps)], 
            dt2[subjectHits(olaps), 
            list(hdd, cdd)])
billweather <- billweather[, list(sumhdd = sum(hdd), 
            sumcdd = sum(cdd)), 
            by=list(startdate, enddate)]

关于r - 在 R 中使用一个数据帧对另一个数据帧中的一系列数据求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15624706/

相关文章:

python - Pandas DataFrame 到控制台格式的 CSV

python - 从长度不均匀的字典创建 pandas 数据框

r - 在 Lavaan 中将协方差设置为零

r - 将经/纬度转换为邮政编码/社区名称

r - 从 R 中的家谱数据生成树状图

r - 如何找到R中不一致和一致对的数量?

将 N/A 替换为 data.table 中的其他值会返回错误

python-3.x - 将 Pandas 系列列表转换为单个 Pandas DataFrame

python - 如何更改xlabel的pandas DataFrame.plot fontsize?

python - 根据 pandas 数据框中的值组合两列