Grum, Mikkel [IPGRI-SSA-Nairobi]
2003-Jan-07 12:54 UTC
[R] Extracting means for given strata from dissimilarity object
Is there a way of extracting mean distance or dissimilarity for a given strata from a 'dist' or 'dissimilarity' object, e.g. extract mean distances for each species in Anderson's iris data? data(iris) iris.dist<-dist(iris[,1:4]) then what? Mikkel Grum, PhD Genetic Diversity Scientist International Plant Genetic Resources Institute (IPGRI) Sub-Saharan Africa Group *** PO Box 30677 Nairobi, Kenya Tel: +254 2 524505(direct)/524500(IPGRI) Fax: +254 2 524501(IPGRI)/524001(ICRAF) m.grum at cgiar.org www.ipgri.org
Martin Maechler
2003-Jan-08 14:08 UTC
[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 <><
Grum, Mikkel [IPGRI-SSA-Nairobi]
2003-Jan-10 08:37 UTC
[R] Extracting means for given strata from dissimilarity object
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 <><