[R] Extracting means for given strata from dissimilarity object

Grum, Mikkel [IPGRI-SSA-Nairobi] M.GRUM at CGIAR.ORG
Fri Jan 10 08:37:03 CET 2003


Thanks Martin. This gives me some ideas, but doesn't quite solve my problem
of getting the mean within-species distances (perhaps I hadn't expressed
that clearly enough).  I'm thinking in lines of something like:

tapply(as.matrix(iris.dist), iris$Species, mean)

except that that doesn't work, because the arguments don't have the same
length and I don't really want to include the dissimilarities of an
individual on to itself (always zero) in the calculation. In real examples I
would often use geographical units as the group that I want the within-group
mean for, as a measure of morphological diversity within the unit.

Any ideas on how to solve this would be greatly appreciated.

Mikkel

-----Original Message-----
From: Martin Maechler [mailto:maechler at stat.math.ethz.ch]
Sent: 08 January 2003 16:07
To: Grum, Mikkel [IPGRI-SSA-Nairobi]
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] Extracting means for given strata from dissimilarity
object


>>>>> "MGrum" == Grum, Mikkel [IPGRI-SSA-Nairobi] <Grum>
>>>>>     on Tue, 07 Jan 2003 03:51:38 -0800 writes:

    MGrum> Is there a way of extracting mean distance or
    MGrum> dissimilarity for a given strata from a 'dist' or
    MGrum> 'dissimilarity' object, e.g. extract mean distances
    MGrum> for each species in Anderson's iris data?

    MGrum> data(iris)
    MGrum> iris.dist<-dist(iris[,1:4])

    MGrum> then what?

mean(iris.dist)   # or
summary(iris.dist)

give the overall statistics; 
for "individual ones", use as.matrix() and apply(), as

## e.g.,
am <- apply(as.matrix(iris.dist), 2, mean)
identical(am, 
          apply(as.matrix(iris.dist), 1, mean) ## -> TRUE


Martin Maechler <maechler at stat.math.ethz.ch>
http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum  LEO C16	Leonhardstr. 27
ETH (Federal Inst. Technology)	8092 Zurich	SWITZERLAND
phone: x-41-1-632-3408		fax: ...-1228			<><




More information about the R-help mailing list