I'm used to using dplyr's lag()
and lead()
in my code, but I'm wondering -- is there a base R alternative?
For example, assume the following dataframe:
df<-data.frame(a=c("a","a","a","b","b"),stringsAsFactors=FALSE)
Using dplyr, I could do this to mark the beginning of a new grouping in a
:
df %>% mutate(groupstart=a!=lag(a)|is.na(lag(a)))
a groupstart
1 a TRUE
2 a FALSE
3 a FALSE
4 b TRUE
5 b FALSE
Is there a way to do this in base R?