Dividing the data into deciles and calculating mean of each decile

Viewed 276

I have two data sets of same dimensions. I want to divide the each row of first data set into deciles based on each row of second data set and then calculate mean of each decile. I know I can do this with the help of order and loops functions. But this will take a lot of time.

x<-matrix(rnorm(10000),nrow = 100,ncol=100)
y<-matrix(rnorm(10000),nrow=100,ncol = 100)
m.r<-rep(0,100)
for (t in 1:100){
  y1<-y[t,]
  my_order<-order(y1,decreasing = T)
  top_10<-my_order[1:10]
  m.r[t]<-mean(x[t,top_10])
}
and so on next 10-20, 20-30 etc.
1 Answers

You could first create a matrix y.order with the row rankings of y.

y.order <- t(apply(y, 1, order, decreasing=TRUE))

Then following the ranking in steps of ten (achieved with split) taking the means in each row within a sapply nesting.

res <- t(sapply(1:nrow(x), function(m) 
  sapply(split(1:100, rep(1:10, each=10)), function(n) 
    mean(x[m, y.order[m, n]]))))

Result

head(res[,1:5])
#               1            2          3            4          5
# [1,] -0.1678216  0.102505130  0.5324668  0.344757153  0.7638530
# [2,]  0.0157764 -0.526831195 -0.3383647 -0.169914681 -0.6141832
# [3,] -0.5376350 -0.001527589 -0.2797928 -0.680547573 -0.1882390
# [4,] -0.1616552 -0.229384402  0.4574600  0.315167214 -0.1952255
# [5,] -0.0478654 -0.095276814  0.4097697 -0.003122267  0.4291888
# [6,]  0.2280566 -0.322672289 -0.5022181 -0.493842480 -0.3193979

Data

set.seed(42)
x <- matrix(rnorm(1e4), nrow=100, ncol=100)
y <- matrix(rnorm(1e4), nrow=100, ncol=100)
Related