Amount if greater than in g

I have a dataframe (obs) with 145 rows and over 1000 columns plus a numeric vector with 145 values ​​(thr).

I would like to get another vector (sumifs) with 145 elements, where each element is the sum of obs [n,]> = thr [n] values.

I thought I could run a for loop where more or less the same sum is calculated:

sumifs[n] <- if(obs[n,]>=thr[n],sum(obs[n,]))

      

but I have not been able to get it to work for one line as well.

I've looked at other questions where it was suggested to use the aggregate or plyr package, but I couldn't find anything.

A simplified example with 15 rows and three columns follows

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

      

so the sumifs should be:

sumifs
5
9
12
15
18
0
0
0
15
18
3
7
9
15
18

      

+3


source to share


1 answer


#your data
DF <- as.data.frame(matrix(1:6, ncol = 2))
#turn into matrix
m <- as.matrix(DF)

#your threshold
thr <- c(3, 1, 7)

#compare
m >= thr
#        V1    V2
#[1,] FALSE  TRUE
#[2,]  TRUE  TRUE
#[3,] FALSE FALSE

#logical values get turned to 0/1 during arithmetics
#thus we can just multiply the matrix with the comparison
m * (m >= thr)
#     V1 V2
#[1,]  0  4
#[2,]  2  5
#[3,]  0  0

#and calculate the row sums
rowSums(m * (m >= thr))
#[1] 4 7 0

      



+5


source







All Articles