I have a the following data set:

data <- cbind(c(1,2,3,4,5,6,7,8,9,10,11),c(1,11,21,60,30,2,61,12,3,35,63))

I would like to select the rows for which the number in the second column is greater than the highest number reached up to that point. The result should look like this.

    [,1]    [,2]
[1,]    1   1
[2,]    2   11
[3,]    3   21
[4,]    4   60
[5,]    7   61
[6,]    11  63
有帮助吗?

解决方案

You want to try cummax:

> d[ d[,2] == cummax(d[,2]) ,]
     [,1] [,2]
[1,]    1    1
[2,]    2   11
[3,]    3   21
[4,]    4   60
[5,]    7   61
[6,]   11   63

PS. data is an internal R function, so, since R variables and functions share the namespace (R design was influenced by Scheme, which is a "Lisp-1"), your variable shadows the system function.

其他提示

The cummax function should work well

data[ data[,2]==cummax(data[,2]),] 

returns

     [,1] [,2]
[1,]    1    1
[2,]    2   11
[3,]    3   21
[4,]    4   60
[5,]    7   61
[6,]   11   63

as desired.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top