简体   繁体   English

R:根据另一个矩阵中的值计算整个矩阵中的百分比值

[英]R:Calculating percentage values across a matrix based on the values in another matrix

I have two matrices, one is a 10x1 double matrix, that can be expanded to any user preset number, eg. 我有两个矩阵,一个是10x1的双矩阵,可以扩展为任何用户预设的数字,例如。 100. 100

View(min_matrx) 查看(min_matrx)

    V1
1   27
2   46
3   30
4   59
5   46
6   45
7   34
8   31
9   52
10  46

The other matrix looks like this, there are more rows not shown: 另一个矩阵如下所示,还有更多行未显示:

View(main_matrx) 查看(main_matrx)

row.names     sum_value
s17           45
s7469         213
s20984        24
s17309        214
s7432369      43
s221320984    12
s17556        34
s741269       11
s20132984     35

For each row name in main_matrx I want to count the number of times that a value more than the sum_value in main_matrx appears in min_matrx. 对于main_matrx中的每个行名称,我要计算min_matrx中出现的值大于main_matrx中的sum_value的次数。 Then I want to divide that by the number of rows in min_matrx and add that value as a new column in main_matrx. 然后,我想将其除以min_matrx中的行数,然后将该值添加为main_matrx中的新列。

For example, in row 1 of main_matrx for s17, the number of times a value appears that is more than 45 in min_matrx =5 times. 例如,在s17的main_matrx的第1行中,一个值出现的次数大于min_matrx = 5次的45次。

Now divide that 5 by 10 rows of min_matrx=> 5/10 =0.5 would be the value I'd like to have as a new column in main_matrx for s17. 现在,将5除以10行min_matrx => 5/10 = 0.5将是我想要作为s17在main_matrx中的新列的值。 Then the same formula for all the s_ids in the row names. 然后对行名称中的所有s_id使用相同的公式。

So far I have fiddled with: 到目前为止,我已经摆弄:

for(s in 1:length(main_matrx)) {
  new<-sum(main_matrx[s,]>min_CPRS_set)/length(min_matrx)
  }

and I tried using apply() but I'm still not getting results. 而且我尝试使用apply(),但仍然没有得到结果。

apply(main_matrx,1:length(main_matrx), function(x) sum(main_matrx>min_CPRS_set)/length(min_matrx)))

Now, I'm just stuck because it's not working. 现在,我被卡住了,因为它无法正常工作。 I'm still new to R so my code isn't particularly efficient. 我对R还是很陌生,所以我的代码并不是特别有效。 Any suggestions? 有什么建议么?

Lots of ways to approach this. 有很多方法可以解决这个问题。 Here's one that came to my head (I think I understand what you're after; again it's much easier to understand an example than with words alone. In the future I'd suggest an example to accompany the text question.) 这是我想到的一个(我想我理解您的追求;再次理解一个示例比单独使用单词要容易得多。将来,我会建议一个示例来解决文本问题。)

Where x is an element, y is a vector 其中x是元素,y是向量

FUN <- function(x, y = min_matrix[, 1]) { 
    sum(y > x)/length(y)
}
main_matrx$new <- sapply(main_matrx[, 2], FUN)

## > main_matrx
##    row.names sum_value new
## 1        s17        45 0.5
## 2      s7469       213 0.0
## 3     s20984        24 1.0
## 4     s17309       214 0.0
## 5   s7432369        43 0.6
## 6 s221320984        12 1.0
## 7     s17556        34 0.6
## 8    s741269        11 1.0
## 9  s20132984        35 0.6

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

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