I have a data that represents assets returns. I want to select top N assets from each row and calculate mean of return of that selected assets. In detail,I want to make a functi
We can use mapply
with asplit
from base R
mapply(function(dat, n) mean(tail(sort(dat), n)), asplit(test_data, 1), top_n)
#[1] 0.8813500 0.2114054 0.2584815 1.2650171 0.2365432 1.0525673
#[7] 0.9391072 0.1261873 0.8011962 1.6519498
set.seed(123)
test_data<- matrix(rnorm(100),nrow=10)
top_n<-c(3,5,9,4,8,7,6,8,3,2)
We can use sapply
to loop over every row, select it's respective top_n
element using tail
and take mean
of it.
sapply(seq_along(top_n), function(x) mean(tail(sort(test_data[x, ]), top_n[x])))
#[1] 0.881 0.211 0.258 1.265 0.237 1.053 0.939 0.126 0.801 1.652
data
set.seed(123)
test_data<- matrix(rnorm(100),nrow=10)
top_n<-c(3,5,9,4,8,7,6,8,3,2)