[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 [EMAIL PROTECTED] Ad Astra per Aspera __ R-help@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.
Re: [R] r function for calculating extreme spread in group
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 __ R-help@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.
Re: [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 1A 10 0.14377148075807 2B 5 -0.117753598165951 3A 14 -0.912068366948338 4B 10 -1.43758624082998 5A 16 -0.797089525071965 6B 11 1.25408310644997 7A 7 0.77214218580453 8B 9 -0.219515626753440 9A 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 [EMAIL PROTECTED] Ad Astra per Aspera [[alternative HTML version deleted]] __ R-help@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.
Re: [R] r function for calculating extreme spread in group
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 1A 10 0.14377148075807 2B 5 -0.117753598165951 3A 14 -0.912068366948338 4B 10 -1.43758624082998 5A 16 -0.797089525071965 6B 11 1.25408310644997 7A 7 0.77214218580453 8B 9 -0.219515626753440 9A 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, '[') AB 8.686382 4.578703 Dylan -- Dylan Beaudette Soil Resource Laboratory http://casoilresource.lawr.ucdavis.edu/ University of California at Davis 530.754.7341 __ R-help@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.
Re: [R] r function for calculating extreme spread in group
Dear Steven, Try this: # Data set yourdata=as.data.frame(cbind(lvl=LETTERS[1:2],x=rpois(10,10),y=rnorm(10) )) # Splitting your data set sdata=split(yourdata,yourdata$lvl) res=lapply(sdata,function(X) max(dist(X[,-1]))) do.call(c,res) HTH, Jorge On Wed, Aug 27, 2008 at 7:28 PM, Steven Matthew Anderson [EMAIL PROTECTED]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 1A 10 0.14377148075807 2B 5 -0.117753598165951 3A 14 -0.912068366948338 4B 10 -1.43758624082998 5A 16 -0.797089525071965 6B 11 1.25408310644997 7A 7 0.77214218580453 8B 9 -0.219515626753440 9A 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 [EMAIL PROTECTED] Ad Astra per Aspera [[alternative HTML version deleted]] __ R-help@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.