I have a dataframe (obs) with 145 rows and more than 1000 columns plus a numeric vector with 145 values (thr).
I would like to derive another vector (sumifs) with 145 elements where each element is the sum of the values of obs[n,] >= thr[n].
I thought I could run a for loop where a single row sum is calculated more or less like:
sumifs[n] <- if(obs[n,]>=thr[n],sum(obs[n,]))
but I didn't manage to make it work for the single row either.
I've been giving a look to other questions where it has been suggested to use aggregate or the plyr package but I didn't really find anything.
A simplified example with only 15 rows and 3 columns is following
c1 <- rep(1:5,3)
c2 <- rep(3:7,3)
c3 <- rep(2:6,3)
obs <- data.frame(r1,r2,r3)
thr <- c(2,2,3,3,4,4,5,5,2,2,3,3,4,4,5)
obs
r1 r2 r3
1 1 3 2
2 2 4 3
3 3 5 4
4 4 6 5
5 5 7 6
6 1 3 2
7 2 4 3
8 3 5 4
9 4 6 5
10 5 7 6
11 1 3 2
12 2 4 3
13 3 5 4
14 4 6 5
15 5 7 6
therefore, sumifs should be:
sumifs
5
9
12
15
18
0
0
0
15
18
3
7
9
15
18