This is another versions of R - How to sum objects in a column between an interval defined by conditions on another column
I have 3 time variables t1, t2 and t3 and a respective column with numbers. I want to sum up the variables from "numbers" together that would fall under time between t1[i]
and t2[i]
. E.g:
t1 <- c(1.12, 2.16, 3.18, 4.56, 8.90, 29.36, 30.30, 31.30, 36.90, 50.01)
t2 <- c(2.14, 2.77, 3.65, 4.78, 8.99, 30.01, 31.07, 31.89, 40.30, 55.08)
t3 <- c(1.16, 1.55, 1.35, 2.17, 2.18, 2.19, 2.34, 3.30, 4.59, 8.91, 29.99, 30.32, 30.98, 31.32, 37.00, 52.00, 54.00)
numbers <- c(7,1,2,5,5,6,9,12, 13, 22, 7, 1, 7, 11, 21, 29)
The output I am looking for the output like this below: Here I have first 3 numbers in t3 satisfy my critera and so on, they are summed up and stored in a new vector "output". PLease note that the "output" here is written by myself and not computed (shown as example). I can compute the first set, however my i stays at the same value and I cannot go on... Hope you can help me, thank you for your time.
output = (7+1+2,5+5+6+9,12,13,22,7,1,7,11,21+29)
output = (10, 25, 12, 13, 22, 7, 1, 7, 11, 50)
So far This is what I have:
t1 <- c(1.12, 2.16, 3.18, 4.56, 8.90, 29.36, 30.30, 31.30, 36.90, 50.01)
t2 <- c(2.14, 2.77, 3.65, 4.78, 8.99, 30.01, 31.07, 31.89, 40.30, 55.08)
t3 <- c(1.16, 1.55, 1.35, 2.17, 2.18, 2.19, 2.34, 3.30, 4.59, 8.91, 29.99, 30.32, 30.98, 31.32, 37.00, 52.00, 54.00)
numbers <- c(7,1,2,5,5,6,9,12, 13, 22, 7, 1, 7, 11, 21, 29)
i = 1
j = 1
k = 1
N = NULL
Sums = NULL
while (j < length(t1))
{
while (i < length(t3))
{
if (t3[i] > t1[j] & t3[i] <= t2[j])
{
N[i] <- numbers[i]
}
i = i + 1
}
Sums[k] = sum(N)
k = k + 1
j = j + 1
}