문제

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