Here is an example of what you can do, adapted from function cass.ind
found inside function mca in package MASS:
cass.ind <- function(cl) {
n <- length(cl)
cl <- as.factor(cl)
x <- matrix(0, n, length(levels(cl)))
x[(1:n) + n * (unclass(cl) - 1)] <- 1
dimnames(x) <- list(names(cl), levels(cl))
x
}
> Data <- data.frame(site = rep(c(1, 2), c(2, 3)),
species = c("A", "B", "A", "B", "C"),
ra = c(1, 3, 1, 2, 3))
> Data
site species ra
1 1 A 1
2 1 B 3
3 2 A 1
4 2 B 2
5 2 C 3
> abund <- function(site, species, abundance)
{
n <- length(species)
species <- as.factor(species)
x <- matrix(0, n, length(levels(species)))
x[(1:n) + n * (unclass(species) - 1)] <- abundance
dimnames(x) <- list(names(species), levels(species))
aggregate(x, list(site = site), sum)
}
> abund(site = Data$site, species = Data$species, abundance = Data$ra)
site A B C
1 1 1 3 0
2 2 1 2 3
Hope this helps,
Renaud
Scott Rollins wrote:
>
> I'm in need of data manipulation help. This is the first time I've
> tried to use S-Plus (version 4.5 running in Windows ME) for cluster
> analysis. I have a dataframe containing 3 columns: site, species, ra
> (relative abundance). I'd like to cluster groups of sites based on
> their species composition, but I'm having trouble converting the data
> into the proper matrix format. Not all sites contain the same species,
> nor equal numbers of species. That is, the ra vector contains no zero
> values, but the site by species matrix will contain many zeros. I'm
> convinced that I can do this in S-Plus and that I don't have to export
> the data to MS Access and run a crosstab query to create the matrix!
> Can anyone help me?
>
> Thanks,
> Scott
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Scott L. Rollins
> Research Assistant
> Algal Ecology Lab
> Department of Zoology
> 203 Natural Science Building
> Michigan State University
> East Lansing, Michigan 48824-1115
> Phone: 517-432-8084
> FAX: 517-432-2789
> e-mail: rollins8@msu.edu
>
> --------------------------------------------------------------------
> This message was distributed by s-news@lists.biostat.wustl.edu. To
> unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
> the BODY of the message: unsubscribe s-news
--
Dr Renaud Lancelot, vétérinaire
CIRAD, Département Elevage et Médecine Vétérinaire (CIRAD-Emvt)
Programme Productions Animales
http://www.cirad.fr/presentation/programmes/prod-ani.shtml (Français)
http://www.cirad.fr/presentation/en/program-eng/prod-ani.shtml (English)
ISRA-LNERV tel (221) 832 49 02
BP 2057 Dakar-Hann fax (221) 821 18 79 (CIRAD)
Senegal e-mail renaud.lancelot@cirad.fr
|