[Dplr-commits] r1128 - in pkg/dplR: R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sat Nov 3 20:14:53 CET 2018
Author: mvkorpel
Date: 2018-11-03 20:14:53 +0100 (Sat, 03 Nov 2018)
New Revision: 1128
Modified:
pkg/dplR/R/as.rwl.R
pkg/dplR/man/as.rwl.Rd
Log:
Improvements to as.rwl
Modified: pkg/dplR/R/as.rwl.R
===================================================================
--- pkg/dplR/R/as.rwl.R 2018-11-03 18:44:54 UTC (rev 1127)
+++ pkg/dplR/R/as.rwl.R 2018-11-03 19:14:53 UTC (rev 1128)
@@ -1,14 +1,19 @@
as.rwl <- function(x){
- if(!(class(x) == "data.frame" | class(x) == "matrix")) {
+ if (!(is.data.frame(x) || is.matrix(x))) {
stop("x must be a data.frame or matrix")
}
- if(class(x) == "matrix") {
+ if (is.matrix(x) ||
+ (!inherits(x, "rwl") && !identical(class(x), "data.frame"))) {
x <- as.data.frame(x)
}
- # are rownames the time vector?
- tmTest <- all(diff(as.numeric(row.names(x))) == 1)
- if(!tmTest) stop("x must have time (years) in the rownames so that all(diff(as.numeric(row.names(x))) == 1)")
- if("rwl" %in% class(x)) TRUE
- class(x) <- c("rwl", "data.frame")
+ ## are rownames the time vector?
+ row_names <- row.names(x)
+ tmTest <- !is.null(row_names) && all(diff(as.numeric(row_names)) == 1)
+ if (!tmTest) {
+ stop("x must have time (years) in the rownames so that all(diff(as.numeric(row.names(x))) == 1)")
+ }
+ if (!inherits(x, "rwl")) {
+ class(x) <- c("rwl", "data.frame")
+ }
x
}
Modified: pkg/dplR/man/as.rwl.Rd
===================================================================
--- pkg/dplR/man/as.rwl.Rd 2018-11-03 18:44:54 UTC (rev 1127)
+++ pkg/dplR/man/as.rwl.Rd 2018-11-03 19:14:53 UTC (rev 1128)
@@ -12,7 +12,7 @@
\item{x}{ a \code{data.frame} or \code{matrix} with series as columns and years as rows }
}
\details{
- This tries to coerce \code{x} into class \code{c("rwl","data,frame")}. Failable.
+ This tries to coerce \code{x} into class \code{c("rwl","data.frame")}. Failable.
}
\value{
An object of class \code{c("rwl", "data.frame")} with the series in
@@ -38,7 +38,7 @@
# coerce to rwl and use plot and summary methods
foo <- as.rwl(foo)
class(foo)
-plot(foo,plot.type="spag")
+plot(foo, plot.type="spag")
summary(foo)
}
\keyword{ manip }
More information about the Dplr-commits
mailing list