Steven Matthew Anderson
2008-Aug-27 20:10 UTC
[R] r function for calculating extreme spread in group
I'm trying to figure out how to write a r function that will calculate
the extreme spread of a group of points given their (x,y)
coordinates. Extreme Spread is the maximal Euclidean distance between
two points in a group
ex.spread = max{ sqrt [ (xi-xj)^2 - (yi-yj)^2 ] } for i not equal to j
I have 60 levels to apply this to.
There is the combination function in the dprep package but I wasn't
sure how to use this or apply an index the points in each group.
Any ideas?
Steve
Steven Matthew Anderson
AdAstra69 at mac.com
Ad Astra per Aspera
Steven Matthew Anderson <adastra69 <at> mac.com> writes:> I'm trying to figure out how to write a r function that will calculate > the extreme spread of a group of points given their (x,y) > coordinates. Extreme Spread is the maximal Euclidean distance between > two points in a group > > ex.spread = max{ sqrt [ (xi-xj)^2 - (yi-yj)^2 ] } for i not equal to j > > I have 60 levels to apply this to. >how about something like max(dist(cbind(x,y)) ... ??? (or to do this in a data frame d with columns x, y, and g (group), sapply(split(d,d$g),function(Z)max(dist(Z[,c("x","y")]))) Ben Bolker
Steven Matthew Anderson
2008-Aug-27 23:28 UTC
[R] r function for calculating extreme spread in group
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.
> Extreme.Spread<-matrix(nrow=60,ncol=2)
> ES<-function(Level) {
+ temp<-
subset(x=ES.Data,Level==level,select=c(Horizontal,Vertical))
+ e<-dist(rbind(temp$Horizontal,temp
$Vertical),method="euclidean")
+ m<-e[which.max(e)]
+ es<-cbind(level,m)
+ Extreme.Spread<-rbind(Extreme.Spread,es)
+ }
> tapply(X=ES.Data,INDEX=Level,FUN=ES)
Error in tapply(X = ES.Data, INDEX = Level, FUN = ES) :
arguments must have same length
On Aug 27, 2008, at 2:30 PM, Jorge Ivan Velez wrote:
> R-help@r-project.org
Steven Matthew Anderson
AdAstra69@mac.com
Ad Astra per Aspera
[[alternative HTML version deleted]]