[R-sig-Geo] complex functions using aggregate

Paul Hiemstra paul.hiemstra at knmi.nl
Thu Sep 15 11:02:14 CEST 2011


 ...purely a cosmetic extension on roberts code, in stead of:

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

I like to use:

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

Paul



On 09/15/2011 01:31 AM, Robert J. Hijmans wrote:
> 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 at 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 at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo


-- 
Paul Hiemstra, Ph.D.
Global Climate Division
Royal Netherlands Meteorological Institute (KNMI)
Wilhelminalaan 10 | 3732 GK | De Bilt | Kamer B 3.39
P.O. Box 201 | 3730 AE | De Bilt
tel: +31 30 2206 494

http://intamap.geo.uu.nl/~paul
http://nl.linkedin.com/pub/paul-hiemstra/20/30b/770



More information about the R-sig-Geo mailing list