[datatable-help] DT 1.9.5 - refers previous/next/whatever row

jim holtman jholtman at gmail.com
Mon Oct 19 16:10:20 CEST 2015


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 <niparisco at gmail.com> 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
> datatable-help at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20151019/dbbdcd56/attachment.html>


More information about the datatable-help mailing list