简体   繁体   English

如果大于r,则总和

[英]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). 我有一个145行和1000多列的数据帧(obs)加上一个145值(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]. 我想得到另一个含有145个元素的向量(sumifs),其中每个元素是obs [n,]> = thr [n]的值的总和。

I thought I could run a for loop where a single row sum is calculated more or less like: 我以为我可以运行一个for循环,其中单行和计算或多或少像:

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. 我一直在寻找其他问题,建议使用聚合或plyr包但我没有找到任何东西。

A simplified example with only 15 rows and 3 columns is following 下面是一个只有15行和3列的简化示例

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: 因此,sumif应该是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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