[R] How to order an data.table by values of an column?

Matthew Dowle mdowle at mdowle.plus.com
Wed Jul 15 01:12:51 CEST 2009


If the question really meant to say "data.table" (i.e. package
"data.table") then its easier than the data.frame answer.

dt = 
data.table(Categ=c(468,351,0,234,117),Perc=c(31.52,27.52,0.77,22.55,15.99))
dt[order(Categ)]

Notice there is no dt$ required before dt$Categ.  Also note the comma is
optional.  See help("[.data.table")

Another example :

dt[Categ>300,cumsum(Perc+Categ)]
[1] 499.52 878.04

Thats it. The i and the j are evaluated within the data.table i.e. you can
use column names as variables in expressions,  like a built-in with() and 
subset().

A join between 2 data.tables X and Y is just X[Y].  This is much faster than 
merge().


"Allan Engelhardt" <allane at cybaea.com> wrote in message 
news:4A309F8E.4000605 at cybaea.com...
> See help("order") and help("[.data.frame").
>
>
> df <- 
> data.frame(Categ=c(468,351,0,234,117),Perc=c(31.52,27.52,0.77,22.55,15.99))
> df[order(df$Categ),]
> #   Categ  Perc
> # 3     0  0.77
> # 5   117 15.99
> # 4   234 22.55
> # 2   351 27.52
> # 1   468 31.52
>
>
> Lesandro wrote:
>> Hello!
>>
>> Can you help me? How to order an data.table by values of an column?
>>
>> Per example:
>>
>> Table no initial
>>
>> Categ Perc
>> 468  31.52
>> 351  27.52
>> 0  0.77
>> 234  22.55
>> 117  15.99
>>
>> table final
>>
>> Categ Perc
>> 0  0.77
>> 117  15.99
>> 234  22.55
>> 351  27.52
>> 468  31.52
>>
>> Lesandro
>>
>>
>>
>>       Veja quais são os assuntos do momento no Yahoo! +Buscados
>>
>> [[alternative HTML version deleted]]
>>
>>   ------------------------------------------------------------------------
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide 
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list