I have worked through a lot of issues in stats and database issues in R and am a new user, but I have not figured out loops and programming yet. I am stuck with this one and just can't figure it out. I have a dataframe that has a start date and an end date and a value TP (total phosphorus).
begin = c("2015/11/16 17:45", "2015/11/17 17:45", "2015/11/18 17:45", "2015/11/19 17:45", "2015/11/20 17:45", "2015/11/21 17:45")
end = c("2015/11/17 17:45", "2015/11/18 17:45", "2015/11/19 17:45", "2015/11/20 17:45", "2015/11/21 17:45", "2015/11/22 17:45")
bottle = c(1, 2, 3, 4, 5, 6)
tp = c(10, 200, 100, 73, 38, 50)
data=data.frame(begin, end, bottle, tp)
I need to multiply TP by the sum of values from another data frame based on the begin and end date for each of the rows. The other data frame is in a shortened version below....
set.seed(1)
time = seq.POSIXt(from=as.POSIXct('2015-11-15',tz=''),
to=as.POSIXct('2016-11-25',tz=''),
by = as.difftime(0.25,units="hours"))
level_m= runif(n = length(time), min = .01, max = .06)
time <- as.data.frame(time)
level_m <- as.data.frame(level_m)
# as dataframe
water_level <- cbind(time, level_m)
I would like to get this to do it for each of the rows of the first data frame called data. What I have tried is using a database approach where the two data frames are merged and aggregated but its not really satisfying as the last value would sum too much data not just the range I need... and it is doing more than I really need.
rng <- cut(water_levle$time,
breaks=c(data$begin, max(data$end)),
include.lowest=T)
test <- aggregate(cbind(count=1, sum=water_level$level_m)~rng, FUN=sum)
I hope I have done this right as it is the first question I have asked here. Thanks, Bill
('data$sum.level <- mapply(function(begin, end, level)
sum(level[begin:end]),
from,
to,
MoreArgs = list(level = water_level$level))
data$tp.load <- data$sum.level * data$tp')
sorry for the multiple edits - trying to do this right – Bill Perry Mar 29 '16 at 13:04