selecting the top n elements from a row and taking their mean

前端 未结 2 1310
时光说笑
时光说笑 2021-01-21 16:14

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

相关标签:
2条回答
  • 2021-01-21 16:51

    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
    

    data

    set.seed(123)
    test_data<- matrix(rnorm(100),nrow=10)
    top_n<-c(3,5,9,4,8,7,6,8,3,2)
    
    0 讨论(0)
  • 2021-01-21 16:58

    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)
    
    0 讨论(0)
提交回复
热议问题