[Dplr-commits] r960 - in pkg/dplR: . R man tests/testthat
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Feb 5 13:18:59 CET 2015
Author: mvkorpel
Date: 2015-02-05 13:18:59 +0100 (Thu, 05 Feb 2015)
New Revision: 960
Modified:
pkg/dplR/ChangeLog
pkg/dplR/R/read.tucson.R
pkg/dplR/man/read.tucson.Rd
pkg/dplR/tests/testthat/test-io.R
Log:
Fixed bug or misfeature which caused read.tucson() to drop leading or
trailing zeros. The old behavior can be restored by setting
edge.zeros=FALSE.
Modified: pkg/dplR/ChangeLog
===================================================================
--- pkg/dplR/ChangeLog 2015-02-05 09:05:08 UTC (rev 959)
+++ pkg/dplR/ChangeLog 2015-02-05 12:18:59 UTC (rev 960)
@@ -62,6 +62,18 @@
- New function for computing the NET parameter (Esper et al., 2001).
+File: read.tucson.R
+-------------------
+
+- New argument 'edge.zeros', TRUE (default) or FALSE. If TRUE,
+ possible leading and trailing zero values in tree-ring series are
+ kept. If FALSE, such values are dropped. The latter is how the
+ function has worked between (pre-)release 1.5.5 and the previoius
+ release, and may be considered a bug or a misfeature. To
+ reiterate, the default behavior has changed in some cases. The
+ alternative, 1.5.5--1.6.2 behavior can be restored by changing the
+ value of the argument.
+
* CHANGES IN dplR VERSION 1.6.2
No functional changes. A unit test was changed so it would not fail
Modified: pkg/dplR/R/read.tucson.R
===================================================================
--- pkg/dplR/R/read.tucson.R 2015-02-05 09:05:08 UTC (rev 959)
+++ pkg/dplR/R/read.tucson.R 2015-02-05 12:18:59 UTC (rev 960)
@@ -1,5 +1,6 @@
`read.tucson` <- function(fname, header = NULL, long = FALSE,
- encoding = getOption("encoding"))
+ encoding = getOption("encoding"),
+ edge.zeros = TRUE)
{
## Checks that the input is good. The input variables are vectors
## ('series', 'decade.yr') or matrices ('x') containing most of
@@ -206,7 +207,7 @@
if (!grepl("\t", data1[length(data1)])) {
## Using a connection instead of a file name in read.fwf and
## read.table allows the function to support different encodings.
- if (long) {
+ if (isTRUE(long)) {
## Reading 11 years per decade allows nonstandard use of stop
## marker at the end of a line that already has 10
## measurements. Such files exist in ITRDB.
@@ -245,8 +246,12 @@
series.fixed <- series
decade.fixed <- decade.yr
x <- as.matrix(dat[3:12])
- ## Convert values <= 0 (not -9999) to NA
- x[x <= 0 & x != -9999] <- NA
+ ## Convert values <= 0 or < 0 (not -9999) to NA
+ if (isTRUE(edge.zeros)) {
+ x[x < 0 & x != -9999] <- NA
+ } else {
+ x[x <= 0 & x != -9999] <- NA
+ }
x.fixed <- x
fixed.ok <- input.ok(series, decade.yr, x)
} else {
@@ -285,7 +290,11 @@
series <- dat[[1]]
decade.yr <- dat[[2]]
x <- as.matrix(dat[3:12])
- x[x <= 0 & x != -9999] <- NA
+ if (isTRUE(edge.zeros)) {
+ x[x < 0 & x != -9999] <- NA
+ } else {
+ x[x <= 0 & x != -9999] <- NA
+ }
if (!input.ok(series, decade.yr, x)) {
if (exists("series.fixed", inherits=FALSE) &&
exists("decade.fixed", inherits=FALSE) &&
Modified: pkg/dplR/man/read.tucson.Rd
===================================================================
--- pkg/dplR/man/read.tucson.Rd 2015-02-05 09:05:08 UTC (rev 959)
+++ pkg/dplR/man/read.tucson.Rd 2015-02-05 12:18:59 UTC (rev 960)
@@ -6,7 +6,7 @@
}
\usage{
read.tucson(fname, header = NULL, long = FALSE,
- encoding = getOption("encoding"))
+ encoding = getOption("encoding"), edge.zeros = TRUE)
}
\arguments{
\item{fname}{ a \code{character} vector giving the file name of the
@@ -26,6 +26,10 @@
problem. Examples of popular encodings available on many systems
are \code{"ASCII"}, \code{"UTF-8"}, and \code{"latin1"} alias
\code{"ISO-8859-1"}. See the help of \code{\link{file}}. }
+ \item{edge.zeros}{ \code{logical} flag indicating whether leading or
+ trailing zeros in series will be preserved (when the flag is
+ \code{TRUE}, the default) or discarded, i.e. marked as \code{NA}
+ (when \code{FALSE}). }
}
\details{
This reads in a standard rwl file as defined according to the
Modified: pkg/dplR/tests/testthat/test-io.R
===================================================================
--- pkg/dplR/tests/testthat/test-io.R 2015-02-05 09:05:08 UTC (rev 959)
+++ pkg/dplR/tests/testthat/test-io.R 2015-02-05 12:18:59 UTC (rev 960)
@@ -188,6 +188,41 @@
expect_equal(0, nrow(read.tucson(tf13, header = FALSE)))
})
+ tf14 <- tempfile()
+ fh14 <- file(tf14, "wt")
+ on.exit(unlink(tf14), add=TRUE)
+ writeLines(c("TST14A 1906 0 0 100 200",
+ "TST14A 1910 300 200 100 200 300 999",
+ "TST14B 1905 300 200 100 200 300",
+ "TST14B 1910 200 100 0 0 999",
+ "TST14C 1906 0 200 100 200",
+ "TST14C 1910 300 200 100 0 999"), fh14)
+ close(fh14)
+ test_that("read.tucson (by default) preserves edge zeros", {
+ res.tf14 <- read.tucson(tf14)
+ expect_true(is.data.frame(res.tf14))
+ expect_named(res.tf14, c("TST14A", "TST14B", "TST14C"))
+ expect_equal(row.names(res.tf14), as.character(1905:1914))
+ expect_equal(res.tf14[[1]],
+ c(NA_real_, 0, 0, 1, 2, 3, 2, 1, 2, 3))
+ expect_equal(res.tf14[[2]],
+ c(3, 2, 1, 2, 3, 2, 1, 0, 0, NA_real_))
+ expect_equal(res.tf14[[3]],
+ c(NA_real_, 0, 2, 1, 2, 3, 2, 1, 0, NA_real_))
+ res.tf14B <- read.tucson(tf14, edge.zeros=FALSE)
+ expect_true(is.data.frame(res.tf14B))
+ expect_named(res.tf14B, c("TST14A", "TST14B", "TST14C"))
+ expect_equal(row.names(res.tf14B), as.character(1905:1914))
+ NA2 <- rep.int(NA_real_, 2)
+ NA3 <- rep.int(NA_real_, 3)
+ expect_equal(res.tf14B[[1]],
+ c(NA3, 1, 2, 3, 2, 1, 2, 3))
+ expect_equal(res.tf14B[[2]],
+ c(3, 2, 1, 2, 3, 2, 1, NA3))
+ expect_equal(res.tf14B[[3]],
+ c(NA2, 2, 1, 2, 3, 2, 1, NA2))
+ })
+
}
test.read.tucson()
### We should write tests for other I/O functions, also
More information about the Dplr-commits
mailing list