Row/column counter in 'apply' functions

后端 未结 5 506
遥遥无期
遥遥无期 2020-12-08 04:32

What if one wants to apply a functon i.e. to each row of a matrix, but also wants to use as an argument for this function the number of that row. As an example,

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-08 05:30

    What I usually do is to run sapply on the row numbers 1:nrow(test) instead of test, and use test[i,] inside the function:

    t(sapply(1:nrow(test), function(i) test[i,]^(1/i)))
    

    I am not sure this is really efficient, though.

提交回复
热议问题