# Re: [R] subtotal, submean, aggregate

From: Patrick Giraudoux <patrick.giraudoux_at_univ-fcomte.fr>
Date: Mon 27 Feb 2006 - 01:40:48 EST

Thanks Roger. Again I learn about a new one: rle(). Though working, get to be a nice Sunday...

Cheers,

Patrick

Roger Bivand a écrit :
> On Sun, 26 Feb 2006, Patrick Giraudoux wrote:
>
>
>> Dear All,
>>
>> I would like to make partial sums (or means or any other function) of
>> the values in intervals along a sequence (spatial transect) where groups
>> are defined.
>>
>> For instance:
>>
>> observations<-rpois(length(habitats),2)
>> transect<-data.frame(observations=observations,habitats=habitats)
>>
>> aggregate() is not suitable for my purpose because I want a result
>> respecting the order of the habitats encountered although they may have
>> the same name (and not pooling each group on each level of the factor
>> created). For instance, the output of the ideal function
>> mynicefunction() would be something as:
>>
>> mynicefunction(transect\$observations, by=list(transect\$habitats),sum)
>> forest 9
>> pasture 17
>>
>> and not
>>
>> aggregate(transect\$observations,by=list(transect\$habitats),sum)

>> Group.1 x
>> 1 forest 9
>> 3 pasture 17
>>
>> Did anybody hear about such a function already written in R? If no, any
>> idea to make it simple and elegant to write?
>>
>
> I got as far as:
>
> rle.habs <- rle(habitats)
> habitats1 <- rep(make.names(rle.habs\$values, unique=TRUE), rle.habs\$lengths)
> aggregate(observations,by=list(habitats1),sum)
>
> making an extra habitats vector with a unique label for each run.
>
> Since I don't know your seed, the results are not the same, but rle() is
> quite good for runs.
>
> Roger
>
>
>> Cheers,
>>
>> Patrick Giraudoux
>>
>> ______________________________________________
>> R-help@stat.math.ethz.ch mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help