سؤال

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