[R] Best way to compute the difference between two levels of a factor ?

S Ellison S.Ellison at LGCGroup.com
Wed Mar 21 15:16:54 CET 2012


 

> -----Original Message-----
> Okay, try this:
> 
>   result <- with(data,
>                  aggregate(data[,-(1:2)], by=list(ID), FUN=diff))
> 
> This assumes that the dataframe is sorted as in your example. 
> If that's not the case, then use order to arrange it first:


A caveat: order and sort will return order of factor levels for factors and that is not necessarily alphanumeric order. The default order here is (T1, T2) but that need not be the case if the levels were allocated explicitly. So make sure that the factor _levels_ are in the order you expect!

Steve E*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}



More information about the R-help mailing list