Neil
Neil

Reputation: 8247

how to compare a current row with all previous rows in r

I have a dataframe in r

  No.   value      time
  1       2         12  
  2       4         23
  3       6         13
  4       8         8
  5       10        13
  6       12        24

My desired dataframe is

 No.    value      time     flag
  1       2         12      0   NA
  2       4         23      0  (4 >= 12)
  3       6         13      0  (6 >= 23,12) 
  4       8         8       0  (8 >= 13,23,12)
  5       10        13      1  (10 >= 8,13,23,12) Satisfied
  6       12        24      1  (12 >= 13,23,12) Satisfied 
  7       14        23      1  

I want to check if current value is greater than or equal to all previous rows of time column and if the condition satisfies it will set the flag to 1.

 df$flag <- ifelse(df$value >= lag(df$time),1,0)

But,this gives me last value to compare not all n previous rows. how can i do it in r ?

Upvotes: 2

Views: 3255

Answers (2)

Joris Meys
Joris Meys

Reputation: 108523

Your output doesn't make sense, as in my book 10 is still smaller than 23. But for the sake of the argument, let's take these to vectors:

set.seed(100)
x <- seq(2,20, by = 2)
y <- sample(4:13)

Then you can easily do what you want using cummax like this:

x >= cummax(y)

The outcome:

> x >= cummax(y)
 [1] FALSE FALSE FALSE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE

> x
 [1]  2  4  6  8 10 12 14 16 18 20

> y
 [1]  7  6  8  4 12  9 13  5 11 10

EDIT: After realizing you asked something completely different to what you actually want, this soludion of d.b would be the solution to get what you need:

c(NA, x[-1] >= cummin(head(y, -1)))
#  [1]    NA FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE

Upvotes: 2

d.b
d.b

Reputation: 32538

as.numeric(c(FALSE, sapply(2:length(df$value), function(i)
    any(df$value[i] >= df$time[1:(i-1)]))))
#[1] 0 0 0 0 1 1 1

DATA

df = structure(list(No. = c(1, 2, 3, 4, 5, 6, 7), value = c(2, 4, 
6, 8, 10, 12, 14), time = c(12, 23, 13, 8, 13, 24, 23)), .Names = c("No.", 
"value", "time"), row.names = c(NA, 7L), class = "data.frame")

Upvotes: 1

Related Questions