I think `shift` is the best option: dt <-data.table(col1=c(1,2,3)) dt[, col3 := shift(col1, type="lag") > 2]
On Mon, Oct 19, 2015 at 10:10 AM, jim holtman <[email protected]> wrote: > does this do what you want: > > > dt <-data.table(col1=c(1,2,3)) > > dt > col1 > 1: 1 > 2: 2 > 3: 3 > > dt[2:nrow(dt), col3:=dt[1:(nrow(dt) - 1), list(col1)]>2] > > dt > col1 col3 > 1: 1 NA > 2: 2 FALSE > 3: 3 FALSE > > > > Jim Holtman > Data Munger Guru > > What is the problem that you are trying to solve? > Tell me what you want to do, not how you want to do it. > > On Mon, Oct 19, 2015 at 9:59 AM, Nicolas Paris <[email protected]> > wrote: > >> Hello, >> >> I wonder if there is a way in data.table (or more generaly in R) to work >> on previous row without loops >> E.G. something equivalent to : >> >> dt <-data.table(col1=c(1,2,3)) >> for (i in 2:nrow(dt)) >> { >> dt[i,col3:=dt[i-1,list(col1)]>2] >> } >> >> Thanks a lot ! >> >> _______________________________________________ >> datatable-help mailing list >> [email protected] >> >> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help >> > > > _______________________________________________ > datatable-help mailing list > [email protected] > https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help >
_______________________________________________ datatable-help mailing list [email protected] https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
