[R] r function for calculating extreme spread in group

Dylan Beaudette dylan.beaudette at gmail.com
Thu Aug 28 02:08:37 CEST 2008


On Wednesday 27 August 2008, Steven Matthew Anderson wrote:
> Thank you Jorge,
>
> My talent in building functions is weak.  I think I'm close but I'm
> not doing something right.
>
> res=dist(yourdata)
> res[which.max(res)]
> does provide me with the correct distance but how do I apply it across
> data with level such as ...
>
>
> yourdata
> =as.data.frame(cbind(lvl=LETTERS[1:2],x=rpois(10,10),y=rnorm(10) ))
>
>  > yourdata
>
>     lvl  x                  y
> 1    A 10   0.14377148075807
> 2    B  5 -0.117753598165951
> 3    A 14 -0.912068366948338
> 4    B 10  -1.43758624082998
> 5    A 16 -0.797089525071965
> 6    B 11   1.25408310644997
> 7    A  7   0.77214218580453
> 8    B  9 -0.219515626753440
> 9    A 12 -0.424810283377287
> 10   B 13 -0.418980099421959
>
> My attempt to use tapply blew up on me.

How about something like:

# generate some fake data
set.seed(1)
x <- as.data.frame(cbind(lvl=LETTERS[1:2],x=rpois(10,10),y=rnorm(10) ))

# function for computing the max, euclidean distance
function(x.i) { d <- dist(x.i) ; d[which.max(d)]}

# run function with data, subset by level
x.list <- by(x, x$lvl, f)

# combine elements of resulting list into vector, with names from original
# levels
sapply(x.list, '[')

       A        B 
8.686382 4.578703 


Dylan


-- 
Dylan Beaudette
Soil Resource Laboratory
http://casoilresource.lawr.ucdavis.edu/
University of California at Davis
530.754.7341



More information about the R-help mailing list