[Dplr-commits] r993 - in pkg/dplR: R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Jun 2 21:43:07 CEST 2015


Author: andybunn
Date: 2015-06-02 21:43:07 +0200 (Tue, 02 Jun 2015)
New Revision: 993

Modified:
   pkg/dplR/R/treeMean.R
   pkg/dplR/man/treeMean.Rd
Log:
In treeMean(), building on Mikko's last comment/commit:

* Renamed first argument to 'x' (was already named so in the
  \arguments section of the .Rd file).

I changed that to "rwl" to be consistent with other functions like detrend


* Inputs are checked.

Having treeMean run naively with no ids is probably a bad idea. I think rather than have ids=NULL be the default we should have it stop with no valid ids object. And yes, I think having the output be "rwl" class is a good idea.



* A warning is produced if there are missing tree IDs, which is
  probably a rare occasion. The series with NA IDs are now
  averaged. Is this desirable or not?  The resulting average may not
  be very useful. Prior to my changes, the function would return a
  completely NA-filled "average" for series with a missing tree ID.

I think a stop here instead.


* Default 'ids' is NULL, means one core per tree (no averaging).

I think a stop here instead.


* Row names of 'ids' (if any) are matched to column names of 'x'.

Good.

* Small optimizations were made.
* Code and examples were reformatted a bit.
* Note: Should the return value inherit class "rwl"?

Yes. I think so. I looked through the help files for read.rwl etc and see that the value in the help file just indicates that a data.frame is returned. Should the help file say that the object is class "rwl" as well as "data.frame"?


Modified: pkg/dplR/R/treeMean.R
===================================================================
--- pkg/dplR/R/treeMean.R	2015-06-02 15:03:06 UTC (rev 992)
+++ pkg/dplR/R/treeMean.R	2015-06-02 19:43:07 UTC (rev 993)
@@ -1,39 +1,34 @@
-treeMean <- function(x, ids = NULL) {
-    ## If 'ids' is NULL then assume one core per tree (no averaging)
-    if (is.null(ids)) {
-        res <- as.data.frame(x)
-        names(res) <- seq_len(length(res))
-        return(res)
-    }
-    x2 <- as.matrix(x)
+treeMean <- function(rwl, ids) {
+    rwl2 <- as.matrix(rwl)
     if (!is.data.frame(ids) || !("tree" %in% names(ids))) {
         stop("'ids' must be a data.frame with column 'tree'")
     }
-    colnames.x <- colnames(x2)
+    colnames.rwl <- colnames(rwl2)
     trees <- as.matrix(ids["tree"])
     rownames.ids <- rownames(trees)
-    ## If all column names in 'x' are present in the set of row
+    ## If all column names in 'rwl' are present in the set of row
     ## names in 'ids', arrange 'ids' to matching order
-    if (!is.null(rownames.ids) && !is.null(colnames.x) &&
-        anyDuplicated(colnames.x) == 0 &&
-        all(colnames.x %in% rownames.ids)) {
-        trees <- trees[colnames.x, ]
-    } else if (length(trees) == ncol(x2)) {
+    if (!is.null(rownames.ids) && !is.null(colnames.rwl) &&
+        anyDuplicated(colnames.rwl) == 0 &&
+        all(colnames.rwl %in% rownames.ids)) {
+        trees <- trees[colnames.rwl, ]
+    } else if (length(trees) == ncol(rwl2)) {
         trees <- as.vector(trees)
     } else {
-        stop("dimension problem: ", "'ncol(x)' != 'nrow(ids)'")
+        stop("dimension problem: ", "'ncol(rwl)' != 'nrow(ids)'")
     }
     uTrees <- unique(trees)
     if (any(is.na(uTrees))) {
         warning("series with missing tree IDs, will be averaged")
     }
     matches <- match(trees, uTrees)
-    res <- matrix(NA_real_, nrow=nrow(x2), ncol=length(uTrees))
+    res <- matrix(NA_real_, nrow=nrow(rwl2), ncol=length(uTrees))
     for (i in seq_along(uTrees)) {
-        res[, i] <- rowMeans(x2[, matches == i, drop=FALSE], na.rm=TRUE)
+        res[, i] <- rowMeans(rwl2[, matches == i, drop=FALSE], na.rm=TRUE)
     }
     res[is.nan(res)] <- NA_real_
-    res <- as.data.frame(res, row.names = rownames(x2))
+    res <- as.data.frame(res, row.names = rownames(rwl2))
     names(res) <- uTrees
+    class(res) <- c("rwl", "data.frame")
     res
 }

Modified: pkg/dplR/man/treeMean.Rd
===================================================================
--- pkg/dplR/man/treeMean.Rd	2015-06-02 15:03:06 UTC (rev 992)
+++ pkg/dplR/man/treeMean.Rd	2015-06-02 19:43:07 UTC (rev 993)
@@ -6,18 +6,17 @@
   This function calculates the mean value for each tree in a rwl or rwi object.
 }
 \usage{
-treeMean(x, ids = NULL)
+treeMean(rwl, ids)
 }
 \arguments{
-  \item{x}{a \code{data.frame} of ring widths with
-    \code{rownames(\var{x})} containing years and \code{colnames(x)}
+  \item{rwl}{a \code{data.frame} of ring widths with
+    \code{rownames(\var{rwl})} containing years and \code{colnames(rwl)}
     containing each series \acronym{ID} such as produced by
     \code{\link{read.rwl}}}
-  \item{ids}{ an optional \code{data.frame} with column one named
+  \item{ids}{ a \code{data.frame} with column one named
     \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.  Defaults to one core per tree as\cr
-    \code{data.frame(tree=1:ncol(\var{rwi}), core=rep(1, ncol(\var{rwi})))}. }
+    for each core. }
 }
 \details{
   Here.
@@ -26,7 +25,7 @@
   A \code{data.frame} with the means of each tree.
 }
 
-\author{ Andy Bunn.  Patched and improved by Mikko Korpela. }
+\author{ Andy Bunn. Patched and improved by Mikko Korpela. }
 
 \seealso{ \code{\link{read.rwl}}, \code{\link{read.ids}}
 }



More information about the Dplr-commits mailing list