[Dplr-commits] r996 - in pkg/dplR: R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Jun 4 23:46:43 CEST 2015
Author: andybunn
Date: 2015-06-04 23:46:43 +0200 (Thu, 04 Jun 2015)
New Revision: 996
Modified:
pkg/dplR/R/treeMean.R
pkg/dplR/man/treeMean.Rd
Log:
Added an argument to treeMean to limit averaging to a period with >1 core in cases where there are two more samples per tree. Just passing na.rm to rowMeans.
Question: what to do if there is no overlap between two cores? E.g., core A runs from 1800 to 1850 and core B from 1852 to 2014. Sometimes (silly) users set their data up this way. This would make internal NA which is a bad practice.
Modified: pkg/dplR/R/treeMean.R
===================================================================
--- pkg/dplR/R/treeMean.R 2015-06-03 20:49:29 UTC (rev 995)
+++ pkg/dplR/R/treeMean.R 2015-06-04 21:46:43 UTC (rev 996)
@@ -1,4 +1,4 @@
-treeMean <- function(rwl, ids) {
+treeMean <- function(rwl, ids, na.rm=FALSE) {
rwl2 <- as.matrix(rwl)
if (!is.data.frame(ids) || !("tree" %in% names(ids))) {
stop("'ids' must be a data.frame with column 'tree'")
@@ -24,7 +24,7 @@
matches <- match(trees, uTrees)
res <- matrix(NA_real_, nrow=nrow(rwl2), ncol=length(uTrees))
for (i in seq_along(uTrees)) {
- res[, i] <- rowMeans(rwl2[, matches == i, drop=FALSE], na.rm=TRUE)
+ res[,i] <- rowMeans(rwl2[, matches == i, drop=FALSE], na.rm=na.rm)
}
res[is.nan(res)] <- NA_real_
res <- as.data.frame(res, row.names = rownames(rwl2))
Modified: pkg/dplR/man/treeMean.Rd
===================================================================
--- pkg/dplR/man/treeMean.Rd 2015-06-03 20:49:29 UTC (rev 995)
+++ pkg/dplR/man/treeMean.Rd 2015-06-04 21:46:43 UTC (rev 996)
@@ -6,7 +6,7 @@
This function calculates the mean value for each tree in a rwl or rwi object.
}
\usage{
-treeMean(rwl, ids)
+treeMean(rwl, ids, na.rm=FALSE)
}
\arguments{
\item{rwl}{a \code{data.frame} of ring widths with
@@ -17,13 +17,21 @@
\code{"tree"} giving a \code{numeric} \acronym{ID} for each tree and
column two named \code{"core"} giving a \code{numeric} \acronym{ID}
for each core. }
+ \item{na.rm}{ \code{logical} passed to \code{\link{rowMeans}}. Should
+ missing values be removed?}
}
\details{
- Here.
+ This function averages together multiple cores to give a mean value of growth.
+ It is very common in dendrochronology to take more than one core per tree. In
+ those cases it is occassionally desirable to have an average of the cores.
+ This function merely loops through the \code{rwl} object and calculates the
+ \code{\link{rowMeans}} for each tree. If \code{na.rm=TRUE} trees with >1
+ sample will be averaged only over the period where the samples overlap.
+ If FALSE the output can vary in the number of samples. See examples.
}
\value{
- An object of class \code{c("rwl", "data.frame")} with the means of
- each tree.
+ An object of class \code{c("rwl", "data.frame")} with the mean annual value
+ for each tree.
}
\author{ Andy Bunn. Patched and improved by Mikko Korpela. }
@@ -33,20 +41,21 @@
\examples{
data(gp.rwl)
gp.ids <- read.ids(gp.rwl, stc = c(0, 2, 1))
+
gp.treeMean <- treeMean(gp.rwl, gp.ids)
-# plot using S3method for class "rwl"
-plot(gp.treeMean,plot.type="spag")
+gp.treeMean2 <- treeMean(gp.rwl, gp.ids, na.rm=TRUE)
+# look at an example of a single tree with different averaging periods
+tree40 <- data.frame(gp.rwl[, c("40A","40B")],
+ gp.treeMean[, "40", drop=FALSE],
+ gp.treeMean2[, "40", drop=FALSE])
+names(tree40) <- c("coreA", "coreB", "treeMean1", "treeMean2")
+head(tree40,50)
+
data(ca533)
ca533.treeMean <- treeMean(ca533, autoread.ids(ca533))
-# look at an example of two cores per tree
-tail(ca533[, c("CAM031", "CAM032"), drop=FALSE])
-tail(rowMeans(ca533[, c("CAM031", "CAM032"), drop=FALSE], na.rm=TRUE))
-tail(ca533.treeMean[, "3", drop=FALSE])
-# look at an example of single tree
-ca533[, "CAM011", drop=FALSE]
-ca533.treeMean[, 1]
-ca533[905, "CAM011"]
-ca533.treeMean[905, 1]
+# plot using S3method for class "rwl"
+plot(ca533.treeMean,plot.type="spag")
+
}
\keyword{ manip }
More information about the Dplr-commits
mailing list