简体   繁体   English

计算大于r中数组列中每个值的值数

[英]count the number of values greater than each value in a column of an array in r

Say I had an array with x representing repeat measurements (1-4), y representing treatments (A,B) and z representing timepoints (1-3) 假设我有一个数组,其中x代表重复测量(1-4),y代表治疗(A,B),z代表时间点(1-3)

x <- c(2,2,4,15,17,13,3,10,3,4,11,14,1,3,19,6,13,6,12,18,9,13,12,16)
dim(x) <- c(4,2,3)

, , 1

     [,1] [,2]
[1,]    2   17
[2,]    2   13
[3,]    4    3
[4,]   15   10

, , 2

     [,1] [,2]
[1,]    3    1
[2,]    4    3
[3,]   11   19
[4,]   14    6

, , 3

     [,1] [,2]
[1,]   13    9
[2,]    6   13
[3,]   12   12
[4,]   18   16

I want to create a new array that has the number of times each replicate is greater than all other replicates for that treatment and timepoint combination: 我想创建一个新数组,该数组具有针对该处理和时间点组合的每个副本的次数大于所有其他副本的次数:

, , 1

     [,1] [,2]
[1,]    2    0 #both 4 and 15 are bigger then 2, so for 1,1,1 the result is 2
[2,]    2    1
[3,]    1    3 #15 is the only replicate bigger than 4 so result for 3,1,1 is 1
[4,]    0    2

, , 2

     [,1] [,2]
[1,]    3    3
[2,]    2    2
[3,]    1    0
[4,]    0    1

, , 3

     [,1] [,2]
[1,]    1    3 
[2,]    3    1 
[3,]    2    2 
[4,]    0    0 

apply can do this, acting within each column (2) and strata (3): apply可以在每一列(2)和层次(3)中起作用:

## recreate your data array:
arr <- c(2,2,4,15,17,13,3,10,3,4,11,14,1,3,19,6,13,6,12,18,9,13,12,16)
dim(arr) <- c(4,2,3)

## one liner using apply
apply(arr, 2:3, function(x) sapply(x, function(y) sum(y < x) ) )

#, , 1
#
#     [,1] [,2]
#[1,]    2    0
#[2,]    2    1
#[3,]    1    3
#[4,]    0    2
#
#, , 2
# 
#     [,1] [,2]
#[1,]    3    3
#[2,]    2    2
#[3,]    1    0
#[4,]    0    1
# 
#, , 3
# 
#     [,1] [,2]
#[1,]    1    3
#[2,]    3    1
#[3,]    2    2
#[4,]    0    0

Here you go... If you're question is incorrectly phrased (as I suspect above), then you will need to use "<" instead of ">". 在这里,您可以...如果您的问题用词不正确(如我在上面怀疑的那样),那么您将需要使用“ <”而不是“>”。

a <- array(rnorm(24), dim= c(4,2,3))

cnts <- function(a) {
  a2 <- array(NA, dim= dim(a))
  for (i in 1:dim(a)[3]) {
    for (j in 1:dim(a)[2]) {
      for (k in 1:length(a[,j,i])) {
        a2[k,j,i] <- sum(a[k,j,i] > a[,j,i])
      }
    }
  }
  return(a2)
}

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

相关问题 计算矩阵中大于某个值的所有值 - Count all values in a matrix greater than a value 计算数组java中大于5.5的所有值 - Count all values greater than 5.5 in an array java Shell脚本:如果数组值大于数字,则运行命令 - shell script : if array value was greater than a number then run a command 在二维数组中添加值,前提是第一个值大于 5 - adding values in a 2d array provided that the first value is greater than 5 如何截断大于指定值的numpy数组? - How to truncate a numpy array for values greater than a specified value? 如何计算大于数字的整数,然后搜索数组以将数字与名称相关 - How to count integers that are greater than a number and then search array to correlate the number to a name php数组选择大于数字且低于另一数字的值并将其保存到新数组 - php numeric array select values greater than a number and lower than another and save it to a new array 计算具有给定值的数组中的值数 - Count number of values in array with a given value 检查数组中的值是否大于数字 - Checking if value in array is greater then a number 附加一个数组中的值,如果另一个数组中的值大于某个值,否则 append 该值 - Appending values from one array, if value in another array is greater than a certain value, else append that value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM