[R] Calculate Range

Jim Lemon jim at bitwrit.com.au
Sun Nov 17 08:12:22 CET 2013

On 11/17/2013 08:49 AM, SCRIPTHAM wrote:
> Hi
> My R version is the current version as at 15 Nov 2013.
> I have tried to calculate range using tapply() with FUN=range.
> tapply() returns two fields, the ID field and a field of two text items one
> is the maximum and the other is the minimum.
> I take as the difference max - min, does R use a different term for range in
> tapply?
> I have also tried
> aggregate() with Fun=range, with Fun=min and FUN=max
> and they also gave problems.
> What is the best route to calculate ranges for groups within a data frame.
Hi Scriptham,
It looks like you want to get the difference between the maximum and 
minimum values rather than the actual values. Define a function:

range_span<-function(x) return(diff(range(x)))

and use that as the FUN argument.


More information about the R-help mailing list