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

Frank Erickson fperickson at wisc.edu
Mon Oct 19 16:12:08 CEST 2015


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 <jholtman at gmail.com> 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 <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
>>
>
>
> _______________________________________________
> 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/da585c68/attachment.html>


More information about the datatable-help mailing list