简体   繁体   中英

sum if greater than in r

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
#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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM