Row/column counter in 'apply' functions

后端 未结 5 510
遥遥无期
遥遥无期 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:21

    cbind()ing the row numbers seems a pretty straightforward approach. For a matrix (or a data frame) the following should work:

    apply( cbind(1:(dim(test)[1]), test), 1, function(x) plot(x[-1], main=x[1]) )
    

    or whatever you want to plot.

提交回复
热议问题