[datatable-help] datatable and function "which.max"

Matthew Dowle mdowle at mdowle.plus.com
Sat Nov 24 23:54:41 CET 2012


Hi,
Welcome to the list.
There's two ways:
y[,.SD[which.max(...)],by=...]
or
y[,.I[which.max(...)],by=...]
See ?data.table for .SD and .I

> Hello,
>
> I hope you can help me with the datatable. My aim is to know the
> position of the daily maximum temperature (Ta).
>
> str(y)
> Classes ‘data.table’ and 'data.frame':  17135 obs. of  6 variables:
>  $ X  : chr  "01/01/2010" "01/01/2010" "01/01/2010" "01/01/2010" ...
>  $ X.1: chr  "0:10:00" "0:20:00" "0:30:00" "0:40:00" ...
>  $ Ta : num  5.28 5.45 5.54 5.54 5.51 5.51 5.5 5.56 5.58 5.63 ...
>  $ HR : int  100 100 100 100 100 100 100 100 100 100 ...
>  $ RS : int  0 0 0 0 0 0 0 0 0 0 ...
>  $ v  : num  2.3 2.5 1.1 2.3 2.2 2.1 2.2 2.9 2.4 2.6 ...
>  - attr(*, ".internal.selfref")=<externalptr>
>
> For this i made the datatable and than:
>
>> z <- y[,which.max(Ta),by=X]
>> z
>               X  V1
>   1: 01/01/2010  84
>   2: 02/01/2010 132
>   3: 03/01/2010  84
>   4: 04/01/2010  92
>   5: 05/01/2010   1
>  ---
> 115: 25/04/2010  92
> 116: 26/04/2010  96
> 117: 27/04/2010  86
> 118: 28/04/2010  90
> 119: 29/04/2010  88
>
>
> it works well, but the index don´t correspond with the index of the
> original table. Have you any idea?
>
> When i try it with z <- y[,max(Ta),by=X] it works perfect, but so I
> don`t know the time,
>
> Thanks for your help.
>
> Best regards,
>
> Dominic
> _______________________________________________
> 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
>




More information about the datatable-help mailing list