r - how to select previous rows in data frame? -


let's have data frame:

df <- data.frame(a=1:5, b=4:8) 

how can subset previous rows? example, if @ row 3, want values of row 1 , row 2, if @ row 4, want values row 2 , row 3. how can in r?

using zoo library (be careful, base lag works differently):

library(zoo) df <- zoo(df) df[lag(df$b, -1) == 4, ]    b 2 2 5  df[lag(df$b, -2) == 4, ]    b 3 3 6 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -