gpt4 book ai didi

r - (日期)内的总和

转载 作者:行者123 更新时间:2023-12-04 22:21:53 33 4
gpt4 key购买 nike

我正在尝试计算发生在同一日期的值的总和(在 XYZmin 中)。

我的数据看起来像这样,

bar <- structure(list(date = structure(c(15622, 15622, 15622, 15628, 
15632, 15635, 15639, 15639, 15639, 15639, 15639, 15642, 15646,
15646, 15650, 15650, 15650, 15657, 15660, 15660, 15674, 15681,
15691, 15695, 15709, 15716, 15723, 15730, 15737, 15737, 15737,
15737, 15737, 15737, 15740, 15743, 15743, 15743, 15744, 15744,
15744, 15744, 15746, 15751, 15755, 15758), class = "Date"), XYZmin = c(-20,
-15, -10, -70, -60, -60, -95, -10, -10, -40, -25, -25, -20, -10,
-3, -5, -25, -5, -70, -5, -30, -30, -25, 60, 60, 60, 60, 60,
-10, -10, -30, -30, -10, -10, -10, -60, -30, -10, 75, -10, -10,
-10, 60, 60, -15, 60)), .Names = c("date", "XYZmin"), class = "data.frame", row.names = c(NA,
-46L))

head(bar)
date XYZmin
1 2012-10-09 -20
2 2012-10-09 -15
3 2012-10-09 -10
4 2012-10-15 -70
5 2012-10-19 -60
6 2012-10-22 -60

我正在努力完成的是创建一个新变量 XYZtot ,其中,在出现不止一次的数据中,对第二个数据的第一个和第二个值求和,并在第三个数据处对第一个、第二个和第三个值求和.这是我的目标的一个片段。
head(new_bar_with_XYZtot) 

date XYZmin XYZtot
1 2012-10-09 -20 -20
2 2012-10-09 -15 -35
3 2012-10-09 -10 -40
4 2012-10-15 -70 -70
5 2012-10-19 -60 -60
6 2012-10-22 -60 -60

更新了大 microbenchmark 测试
alexwhan <- function(bar,date,XYZmin) ddply(bar, .(date), transform, XYZmin.sum = cumsum(XYZmin))

Arun <- function(bar,date,XYZmin) within(bar, {XYZtot <- ave( XYZmin, date, FUN=cumsum)})

agstudy <- function(bar,date,XYZmin) transform(bar, XYZtot = ave(XYZmin, date, FUN = cumsum))

# install.packages("data.table", dependencies = TRUE)
library(data.table)
mnel <- function(bar,date,XYZmin) bar <- data.table(bar); bar[, XYZmin.sum := cumsum(XYZmin), by = date]

# install.packages("microbenchmark", dependencies = TRUE)
require(microbenchmark)

# run test
res <- microbenchmark(alexwhan(bar,date,XYZmin), Arun(bar,date,XYZmin), agstudy(bar,date,XYZmin), mnel(bar,date,XYZmin), times = 666)


## Print results:
print(res)

数字,
Unit: microseconds
expr min lq median uq max neval
alexwhan(bar, date, XYZmin) 14484.077 15056.613 15237.760 15945.482 72650.126 666
Arun(bar, date, XYZmin) 963.632 1018.311 1070.759 1138.655 4988.226 666
agstudy(bar, date, XYZmin) 1967.292 2021.115 2078.261 2158.689 9240.500 666
mnel(bar, date, XYZmin) 251.312 270.295 282.821 325.040 6540.367 666


### Plot results:
boxplot(res)

最佳答案

也使用 ave 但使用 transform

transform(bar, XYZtot = ave(XYZmin, date, FUN = cumsum))

在 OP 评论后编辑
transform(bar, XYZtot = ave(XYZmin, date, FUN = 
function(x)
if(length(x) < 1) NA
else c(cumsum(x[-length(x)]),NA)))

关于r - (日期)内的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15371544/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com