Ben, perhaps this will do it:

fun <- function(x) { sum(abs(x[-length(x)] - x[-1])) }

agg<-aggregate(Elevp$Elevation,by=list(Elevp$From,Elevp$To), fun)

Robert


On Wed, Sep 14, 2011 at 4:17 PM, Ben Weinstein
<benweinstein2010@gmail.com>wrote:

> Hi all
>
> I have a series of elevation transects between a list of pairwise
> locations.
> This is not specifically a spatial question, but since it is a common
> spatial task, i hope its okay.
>
> head(Elevp)
> Elevation ComboID From  To
> 1  3408.080       1  262 138
> 2  3264.469       1  262 138
> 3  2904.685       1  262 138
> 4  2752.840       1  262 138
> 5  3408.080       2  262 129
> 6  3217.581       2  262 129
>
> In the small section above, the first 'transect' has four points, and goes
> from site 262 to site 138. I'm trying to find a way to get the total
> elevation change over each transect. An abstract example is shown below.
>
> > h
> [1]  10   5  40  30 100  60
> > g<-numeric()
> > for(x in 1:(length(h)-1))
> + g[x]<-((abs(h[x+1]-h[x])))
> > g
> [1]  5 35 10 70 40
> > sum(g)
> [1] 160
>
> to perform this over the entire database (1000's of comparisons)
>
> agg<-aggregate(Elevp$Elevation,by=list(Elevp$From,Elevp$To),function(x)
>
> how do i supply the former function, as a product that fits this setup?
> Does a cumulative difference function exist? I'm not sure what to call it
> to
> begin searching.
>
> I appreciate the help.
>
> Ben Weinstein
> Ecology and Evolution
> Stony Brook University
>
>        [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

	[[alternative HTML version deleted]]

