[datatable-help] Remove some data table rows based on three conditions

Chris Neff caneff at gmail.com
Wed Oct 22 19:38:23 CEST 2014


DT[, .SD[which.max(marker),], by=.(id, datep)]

is what I would do.



On Wed Oct 22 2014 at 1:19:17 PM Frank S. <f_j_rod at hotmail.com> wrote:

> Dear all,
> I'm working with a large database in wich I have some rows which have
> identical id and datep variables. Of these
> duplicated rows, I only want to keep those row associated to the maximum
> value in marker variable. As an example:
> DT <- data.table(
>  id = rep(c(2,5),c(3,2)),
>  datep = as.Date(c('1995-04-20','1995-04-20',
> '1997-02-19', '1998-01-15','1998-01-15')),
>  marker = c(2,8,5,7,5),
>  group=rep(c("A","B"),c(3,2))
>  )
> First, I sort by key variables: id, marker
> DT[order(id,marker)]
>
> But afterwards I've tried different things and I'm not able to what I want:
> DT[!duplicated(DT[c('id', 'datep')])]
> DT[ !(duplicated %chin% c('id','datep'))]
> DT[ !(duplicated %in% c('id','datep'))]
> DT[,!(duplicated(DT[c("id","datep")])), by=list(id,datep)]
> unique(DT[c('id','datep')])
> Please, does anyone know how to do it?
>  _______________________________________________
> 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/20141022/b0f7e779/attachment.html>


More information about the datatable-help mailing list