Permalink
Browse files

Edited [<- method for Periods to allow correct assignment to periods …

…within vectors and data.frames new method replaces all attributes of a period. Previous method just replaced seconds value. Fixes #119.
  • Loading branch information...
1 parent cb8e3d4 commit d8144c839d20c1043ade369b3c57b012cfe401ae @garrettgman garrettgman committed Sep 21, 2012
Showing with 19 additions and 7 deletions.
  1. +4 −4 NAMESPACE
  2. +3 −3 R/periods.r
  3. +12 −0 inst/tests/test-periods.R
View
@@ -64,7 +64,6 @@ export("week<-")
export("yday<-")
export("year<-")
export(am)
-export(as.difftime)
export(as.duration)
export(as.interval)
export(as.period)
@@ -110,7 +109,6 @@ export(int_end)
export(int_flip)
export(int_shift)
export(int_start)
-export(intersect)
export(interval)
export(is.Date)
export(is.POSIXct)
@@ -165,13 +163,11 @@ export(reclass_timespan)
export(round_date)
export(second)
export(seconds)
-export(setdiff)
export(stamp)
export(stamp_date)
export(stamp_time)
export(today)
export(tz)
-export(union)
export(wday)
export(week)
export(weeks)
@@ -197,6 +193,7 @@ exportMethods("$")
exportMethods("$<-")
exportMethods("%%")
exportMethods("%/%")
+exportMethods("%within%")
exportMethods("*")
exportMethods("+")
exportMethods("-")
@@ -209,9 +206,12 @@ exportMethods(">=")
exportMethods("[")
exportMethods("[<-")
exportMethods(as.difftime)
+exportMethods(as.duration)
exportMethods(as.numeric)
+exportMethods(as.period)
exportMethods(c)
exportMethods(intersect)
+exportMethods(reclass_timespan)
exportMethods(rep)
exportMethods(setdiff)
exportMethods(show)
View
@@ -191,8 +191,8 @@ setMethod("c", signature(x = "Period"), function(x, ...){
years <- c(x@year, unlist(lapply(list(...), slot, "year")))
months <- c(x@month, unlist(lapply(list(...), slot, "month")))
days <- c(x@day, unlist(lapply(list(...), slot, "day")))
- hours <- c(x@month, unlist(lapply(list(...), slot, "hour")))
- minutes <- c(x@month, unlist(lapply(list(...), slot, "minute")))
+ hours <- c(x@hour, unlist(lapply(list(...), slot, "hour")))
+ minutes <- c(x@minute, unlist(lapply(list(...), slot, "minute")))
new("Period", seconds, year = years, month = months, day = days,
hour = hours, minute = minutes)
})
@@ -212,7 +212,7 @@ setMethod("[", signature(x = "Period"),
})
#' @export
-setMethod("[<-", signature(x = "Period", i = "Period"),
+setMethod("[<-", signature(x = "Period", value = "Period"),
function(x, i, j, ..., value) {
x@.Data[i] <- value@.Data
x@year[i] <- value@year
View
@@ -103,3 +103,15 @@ test_that("as.period handles duration objects", {
expect_that(as.period(dur), equals(seconds(5) + minutes(30)))
})
+test_that("[<- can subset periods with new periods", {
+ Time <- data.frame(Time = c(hms("01:01:01"), hms("02:02:02")))
+ Time[1,1] <- Time[1,1] + hours(1)
+
+ times <- days(1:3)
+ times[1] <- times[1] + hours(2)
+
+ expect_equal(Time[1,1], hms("02:01:01"))
+ expect_equal(times[1], new_period(days = 1, hours = 2))
+
+})
+

0 comments on commit d8144c8

Please sign in to comment.