我有date_time字段,其中包含分钟范围(第一分钟,第二分钟......)。
我想每小时整合theMass
。
我必须考虑vs
和t_in
每小时的平均值(theMass取决于t_in并显示与vs相比)才能正确计算。
代码给了我24个结果(每小时一次积分),但你可以看到结果是线性增加,这是不对的。
所以,我不确定我的计算方法,或者是否有另一种方法可以实现这一目标。
library(tidyverse)
library(Bolstad2)
thelength = 1440
date_time <- seq(1, thelength)
fo <- runif(thelength, 1000, 3000)
vs <- runif(thelength, 0, 16)
t_in <- runif(thelength, 0, 150)
A3 <- 1.998201136e-10
A2 <- -1.467393126e-07
A1 <- -0.000681555
A0 <- 1.010255780
dens <- 0.9859
# Mass in kgr/h
theMass <- fo * dens * (A3 * (t_in ^ 3) + A2 * (t_in ^ 2) + A1 * t_in + A0)
df <- data.frame(date_time, fo, vs, t_in, theMass)
# because date_time is by 1 min, we want
# to integrate every hour, so by 60 values
df <- df %>%
group_by(cut(date_time, breaks = seq(0, thelength, by = 60))) %>%
summarise_at(vars(t_in, vs), mean, na.rm = TRUE)
df <- na.omit(df)
per_hour <- 60
hours <- dim(df)[1]
# result of integration
integration <- vector('list', hours)
# Mass for first hour
theMass_1 <- fo * dens * (A3 * (df$t_in[1] ^ 3) +
A2 * (df$t_in[1] ^ 2) +
A1 * df$t_in[1] + A0)
# integration for 1 hour
integration[[1]] <- sintegral(date_time[1:length(seq(1, per_hour))],
theMass_1[1:length(seq(1, per_hour))])
# integration for the rest hours
for (i in 2:hours)
{
# add 60 minutes at every hour loop
if (i > 2)
{
per_hour <- 60 + per_hour
}
# integrate by every hour
integration[[i]] <- sintegral(date_time[1:length(seq(per_hour, 2 * per_hour))],
theMass[1:length(seq(per_hour, 2 * per_hour))])
}
# the result should hold only the integration value for every hour
result <- c()
for (i in 1:length(integration))
{
result[i] <- integration[[i]]$int
}
result
答案 0 :(得分:1)
问题发生在1:length(seq(per_hour, 2 * per_hour))
,随着per_hour
的增加而变长,并始终从1开始。
我认为在你的循环中你需要per_hour + 1:60
。或者只是(i-1)*60 + 1:60