Skip to content

Commit

Permalink
Add methods to convert between data.frame and twdtwTimeSeries
Browse files Browse the repository at this point in the history
  • Loading branch information
vwmaus committed Oct 23, 2019
1 parent 8066c7e commit 83cd058
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ export(twdtwApplyParallel)
export(twdtwClassify)
exportMethods("[")
exportMethods("[[")
exportMethods(as.data.frame)
exportMethods(as.list)
exportMethods(as.twdtwTimeSeries)
exportMethods(bands)
exportMethods(coordinates)
exportMethods(coverages)
Expand Down Expand Up @@ -84,6 +86,7 @@ import(zoo)
importFrom(RColorBrewer,brewer.pal)
importFrom(Rdpack,reprompt)
importFrom(caret,createDataPartition)
importFrom(data.table,rbindlist)
importFrom(dtw,asymmetric)
importFrom(dtw,rabinerJuangStepPattern)
importFrom(dtw,symmetric1)
Expand Down
23 changes: 23 additions & 0 deletions R/methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ setGeneric("layers",
setGeneric("coverages",
function(x) standardGeneric("coverages"))

setGeneric("as.twdtwTimeSeries",
function(x) standardGeneric("as.twdtwTimeSeries"))

setGeneric("bands",
function(x) standardGeneric("bands"))

Expand All @@ -33,6 +36,14 @@ setGeneric("is.twdtwRaster",
setGeneric("projecttwdtwRaster",
function(x, ...) standardGeneric("projecttwdtwRaster"))

as.data.frame.twdtwTimeSeries <- function(x){
lapply(x[], function(y){
out <- data.frame(date = index(y), y)
rownames(out) <- NULL
return(out)
})
}

as.list.twdtwTimeSeries = function(x) lapply(seq_along(x), function(i)
new("twdtwTimeSeries", x[[i]], labels(x)[i]) )

Expand Down Expand Up @@ -296,6 +307,11 @@ setMethod("as.list", "twdtwMatches", as.list.twdtwMatches)
#' @export
setMethod("as.list", "twdtwRaster", as.list.twdtwRaster)

#' @inheritParams twdtwTimeSeries-class
#' @rdname twdtwTimeSeries-class
#' @export
setMethod("as.data.frame", "twdtwTimeSeries", as.data.frame.twdtwTimeSeries)

#' @inheritParams twdtwTimeSeries-class
#' @param i indices of the time series.
#' @rdname twdtwTimeSeries-class
Expand Down Expand Up @@ -575,6 +591,13 @@ setMethod(f = "show", "twdtwMatches",
setMethod(f = "show", "twdtwRaster",
definition = show.twdtwRaster)

#' @aliases as.twdtwTimeSeries
#' @inheritParams twdtwTimeSeries-class
#' @describeIn twdtwTimeSeries convert list of data.frame to class twdtwTimeSeries.
#' @export
setMethod("as.twdtwTimeSeries", "ANY",
function(x) twdtwTimeSeries(lapply(x[], function(y) zoo(y[, names(y)!="date"], order.by = y$date))))

#' @aliases is.twdtwTimeSeries
#' @inheritParams twdtwTimeSeries-class
#' @describeIn twdtwTimeSeries Check if the object belongs to the class twdtwTimeSeries.
Expand Down
Binary file added inst/lucc_MT/patterns/patt.rds
Binary file not shown.
9 changes: 9 additions & 0 deletions man/twdtwTimeSeries-class.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 83cd058

Please sign in to comment.