如果大于 r 则求和 [英] sum if greater than in r

查看:34
本文介绍了如果大于 r 则求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 145 行和 1000 多列的数据框 (obs),以及一个包含 145 个值 (thr) 的数字向量.

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

我想导出另一个包含 145 个元素的向量 (sumifs),其中每个元素是 obs[n,] >= thr[n] 的值之和.

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].

我想我可以运行一个 for 循环,其中单行总和的计算或多或少像:

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.

我一直在查看其他建议使用聚合或 plyr 包的问题,​​但我没有真正找到任何东西.

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.

下面是一个只有 15 行 3 列的简化示例

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

因此,sumifs 应该是:

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

这篇关于如果大于 r 则求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆