[Ecopd-commits] r88 - in branches/single-tree: R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Nov 18 21:04:12 CET 2009


Author: regetz
Date: 2009-11-18 21:04:11 +0100 (Wed, 18 Nov 2009)
New Revision: 88

Modified:
   branches/single-tree/R/community.R
   branches/single-tree/man/siteBySpecies.Rd
Log:
rewrote siteBySpecies to work with phylo4com objects; updated doc


Modified: branches/single-tree/R/community.R
===================================================================
--- branches/single-tree/R/community.R	2009-11-18 19:44:06 UTC (rev 87)
+++ branches/single-tree/R/community.R	2009-11-18 20:04:11 UTC (rev 88)
@@ -1,23 +1,14 @@
 # Generate site-by-species matrix
-siteBySpecies <- function(phylo4com, presence.only=FALSE,
-  transpose=FALSE) {
+siteBySpecies <- function(phy, presence.only=FALSE,
+    na.zero=FALSE, transpose=FALSE) {
 
-  ## create and populate the matrix
-  spp <- unique(unlist(lapply(phylo4com, tipLabels)))
-  mat <- sapply(phylo4com, function(x) {
-    vec <- tipData(x)[spp, "abundance"]
-    vec[!spp %in% tipLabels(x)] <- 0
-    vec
-  })
-  rownames(mat) <- spp
+    if (presence.only) {
+        dat <- presence(phy, na.zero=na.zero)
+    } else {
+        dat <- abundance(phy, na.zero=na.zero)
+    }
+    mat <- as.matrix(dat)
+    if (!transpose) mat <- t(mat)
+    return(mat)
 
-  if (presence.only) {
-    mat[mat>0] <- 1
-    mat[mat<=0] <- 0
-  }
-
-  if (!transpose) mat <- t(mat)
-
-  return(mat)
-
 }

Modified: branches/single-tree/man/siteBySpecies.Rd
===================================================================
--- branches/single-tree/man/siteBySpecies.Rd	2009-11-18 19:44:06 UTC (rev 87)
+++ branches/single-tree/man/siteBySpecies.Rd	2009-11-18 20:04:11 UTC (rev 88)
@@ -2,42 +2,41 @@
 \alias{siteBySpecies}
 \title{Generate site-by-species matrix}
 \description{
-  Returns a site-by-species matrix of abundance values from a phylo4com
-  list.
+  Returns a site-by-species matrix of abundance or presence/absence
+  values from a phylo4com object.
 }
 \usage{
-siteBySpecies(phylo4com, presence.only=FALSE, transpose=FALSE)
+  siteBySpecies(phy, presence.only=FALSE, na.zero=FALSE, transpose=FALSE)
 }
 \arguments{
-  \item{phylo4com}{A list of phylo4d objects with abundance data}
-  \item{presence.only}{(logical) Should abundance values be replaced
-    with 1/0 indicators of presence/absence? Default FALSE.}
-  \item{transpose}{(logical) Should matrix be transposed to put 
-    species in rows and sites in columns? Default FALSE.}
+  \item{phy}{a \code{phylo4com} object}
+  \item{presence.only}{logical. Should abundance values be replaced
+    with 1/0 indicators of presence/absence?}
+  \item{na.zero}{logical. should NAs be treated as zero abundances?}
+  \item{transpose}{logical. Should matrix be transposed to put 
+    species in rows and sites in columns?}
 }
 \details{
-  Abundance values are taken from the \dQuote{abundance} data column in
-  each \code{phylo4d} tree. Any \code{NA} values in the abundance data
-  will be preserved in the matrix. However, abundance is assumed to be 0
-  for all species absent from the \code{phylo4d} tree for a given site.
+  If \code{na.zero} is false (the default), any \code{NA} values in the
+  abundance data will be preserved in the matrix. However, abundance is
+  assumed to be 0 for all species absent from the subtree for a given
+  site.
 
-  If \code{presence.only=TRUE}, the matrix will contain 1 for species
+  If \code{presence.only} is true, the matrix will contain 1 for species
   with positive abundances in the corresponding tree, and 0 both for
   species with non-positive abundances and for species absent from the
-  tree for a given site. Any \code{NA} values in the abundance data will
-  remain as \code{NA} in the matrix.
+  tree for a given site. As with abundance, if \code{na.zero} is false,
+  \code{NA} values in the abundance data will remain as \code{NA} in the
+  matrix.
 
-  Species names are taken directly from tip labels in the \code{phylo4d}
-  trees, and the total number of species in the matrix will equal the
-  number of unique tip labels across all trees. Site names are taken
-  directly from the names of elements in the \code{phylo4com} list, and
-  the number of sites is simply equal to the number of \code{phylo4d}
-  objects. 
-
   In the default orientation, species will be in columns and sites in
   rows. To swap rows and columns, use \code{transpose=TRUE} (or manually
   transpose the returned object).
 }
+\seealso{
+  This function is just a convenience wrapper for the
+  \code{\link{abundance}} and \code{\link{presence}} functions.
+}
 \value{
   Numeric matrix 
 }
@@ -48,6 +47,6 @@
   siteBySpecies(weeds, presence.only=TRUE)
 
   ## change an abundance value to NA
-  abundance(weeds[[1]])[1] <- NA
+  abundance(weeds, "A", 1) <- NA
   siteBySpecies(weeds, presence.only=TRUE, transpose=TRUE)
 }



More information about the Ecopd-commits mailing list