[datatable-help] Filtering Based on Previous Observation
Arunkumar Srinivasan
aragorn168b at gmail.com
Wed Apr 30 11:01:54 CEST 2014
Perhaps do:
DT[, GRP := .GRP, by=a]
And then continue as Chinmay's earlier solution on the column GRP?
Arun
From: Michael Smith my.r.help at gmail.com
Reply: Michael Smith my.r.help at gmail.com
Date: April 30, 2014 at 11:00:21 AM
To: datatable-help at lists.r-forge.r-project.org datatable-help at lists.r-forge.r-project.org
Subject: Re: [datatable-help] Filtering Based on Previous Observation
... and here's a new challenge. What if `a` is character?
DT <- data.table(a = as.character(c(1, 2, 2, 3)), b = 8:5)
DT[, L.a := c(NA, head(a, -1))][a != L.a | is.na(L.a)][, L.a := NULL][]
I still would like to compare equality with the previous
observation/row, and remove it if it's the same. (`diff` doesn't work
with characters.)
M
On 04/30/2014 04:55 PM, Michael Smith wrote:
> Chinmay,
>
> Kudos, that's a nice one! It also can be generalized to longer lags.
> Thanks!
>
> M
>
> On 04/30/2014 03:59 PM, Chinmay Patil wrote:
>> You can try
>>
>> DT
>> ## a b
>> ## 1: 1 8
>> ## 2: 2 7
>> ## 3: 2 6
>> ## 4: 3 5
>>
>> DT[c(T,diff(a)!=0),]
>> ## a b
>> ## 1: 1 8
>> ## 2: 2 7
>> ## 3: 3 5
>>
>>
>>
>>
>> On Tue, Apr 29, 2014 at 10:04 PM, Michael Smith <my.r.help at gmail.com
>> <mailto:my.r.help at gmail.com>> wrote:
>>
>> All,
>>
>> Is there some data.table-idiomatic way to filter based on a previous
>> observation/row? For example, I want to remove a row if
>> DT$a[row]==DT$a[row-1].
>>
>> It could be done by first calculating the lag and then filtering based
>> on that, but I wonder if there's a more direct way.
>>
>> The following example works, but my feeling is there should be a more
>> elegant solution:
>>
>> ( DT <- data.table(a = c(1, 2, 2, 3), b = 8:5) )
>> DT[, L.a := c(NA, head(a, -1))][a != L.a | is.na
>> <http://is.na>(L.a)][, L.a := NULL][]
>>
>> Thanks,
>> M
>> _______________________________________________
>> datatable-help mailing list
>> datatable-help at lists.r-forge.r-project.org
>> <mailto: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/20140430/f8c659fe/attachment.html>
More information about the datatable-help
mailing list