R lubridate - error with using apply and minute -
R lubridate - error with using apply and minute -
i have function working period objects in lubridate package. specifically, i'm getting errors using minute() function while using min within apply-ie: apply(data, 2, minute).
the function i've written wrapper utilize mean, sd, etc period objects after converting them numeric (count of seconds). here's code:
conv_ms <- function(x, func= "mean", na.rm=true, ...) { # extract min/sec, convert numeric m <- minute(x) s <- second(x) mns <- m * 60 + s # apply wrapper if (func == "mean") { x2 <- mean(mns, na.rm= na.rm) } else if (func == "sd") { x2 <- sd(mns, na.rm= na.rm) } else if (func == "median") { x2 <- median(mns, na.rm= na.rm) } else if (func == "quantile") { x2 <- quantile(mns, na.rm= na.rm, ...) } else if (func == "min") { x2 <- min(mns, na.rm= na.rm) } else if (func == "max") { x2 <- max(mns, na.rm= na.rm) } m2 <- trunc(x2 / 60) s2 <- round((x2 - trunc(x2 / 60)) * 60, 0) return(ms(paste(as.character(m2), as.character(s2), sep=":"))) } and execution. when execute function, error using minute(). but minute runs when outside apply framework.
# run above function -- error message , traceback conv_ms(men_g[, c(10:11)], na.rm=t) error in as.posixlt.default(x, tz = tz(x)) : not know how convert 'x' class “posixlt” 5 stop(gettextf("do not know how convert '%s' class %s", deparse(substitute(x)), dquote("posixlt")), domain = na) 4 as.posixlt.default(x, tz = tz(x)) 3 as.posixlt(x, tz = tz(x)) 2 minute.default(x) 1 conv_ms(men_g[, c(10:11)], na.rm = t) # isolate error -- show error message # appears not work within apply apply(men_g[, c(10:11)], 2, minute) error in as.posixlt.numeric(x, tz = tz(x)) : 'origin' must supplied # min works on isolated vector minute(men_g[, 10]) [1] 20 20 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 20 21 21 21 20 21 21 20 21 21 21 21 21 [35] 21 21 21 21 21 21 21 22 22 data -- edited utilize dput
dput(men_g[,10:11]) structure(list(ev6_raw = structure(c(29, 53, 27, 14, 6, 34, 51, 6, 18, 15, 5, 4, 7, 34, 11, 51, 58, 19, 39, 8, 30, 56, 14, 37, 9, 24, 46, 28, 51, 39, 25, 51, 40, 50, 45, 36, 52, 18, 46, 40, 36, 16, 42), year = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), month = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), day = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), hr = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ), min = c(20, 20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 20, 21, 21, 21, 20, 21, 21, 20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22), class = structure("period", bundle = "lubridate")), ev7_raw = structure(c(44, 52, 38, 3, 55, 46, 7, 54, 6, 5, 12, 22, 0, 56, 23, 56, 45, 6, 24, 1, 5, 10, 24, 10, 27, 31, 33, 57, 19, 43, 4, 4, 11, 10, 53, 13, 10, 22, 21, 6, 58, 52, 51), year = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), month = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), day = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), hr = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), min = c(1, 1, 1, 2, 1, 1, 2, 1, 2, 2, 2, 2, 2, 1, 2, 1, 1, 3, 2, 2, 2, 3, 2, 2, 2, 1, 2, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 1, 2), class = structure("period", bundle = "lubridate"))), .names = c("ev6_raw", "ev7_raw"), class = "data.frame", row.names = c(1l, 2l, 3l, 4l, 5l, 6l, 7l, 8l, 9l, 11l, 12l, 13l, 14l, 16l, 17l, 18l, 19l, 20l, 21l, 22l, 23l, 24l, 25l, 26l, 30l, 31l, 33l, 34l, 35l, 39l, 41l, 42l, 43l, 44l, 53l, 54l, 67l, 68l, 70l, 72l, 82l, 83l, 165l))
when see error message, please think says, rather throw hands , "it doesn't work". helps spell class ('period') know functions apply when using methods or getmethods.
> methods(minute) [1] minute.default* minute.period* non-visible functions asterisked > help(pack='lubridate') showmethods(classes="period") # extensive output not included the "period" objects above apparently list objects bunch of attributes hold information. 1 of reasons posixlt objects problematic not take beingness passed apply-function because, coerces items atomic vectors. posixlt objects , period-objects should carry values remaining attached attributes lists. when attributes stripped period-object, info minutes, days, months, , years removed.
i sense may have messed before since period-classed objects supposed s4 objects slots , have ordinary s3 sort of list info in attributes"
men_g[[1]] [1] 29 53 27 14 6 34 51 6 18 15 5 4 7 34 11 51 58 19 39 8 30 56 14 37 9 24 46 28 51 39 25 51 [33] 40 50 45 36 52 18 46 40 36 16 42 attr(,"year") [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 attr(,"month") [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 attr(,"day") [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 attr(,"hour") [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 attr(,"minute") [1] 20 20 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 20 21 21 21 20 21 21 20 21 21 21 [33] 21 21 21 21 21 21 21 21 21 22 22 attr(,"class") [1] "period" attr(,"class")attr(,"package") [1] "lubridate" what period object should under str-microscope:
> span <- new_interval(as.posixct("2009-01-01"), as.posixct("2010-02-02 01:01:01")) #interval > # 2009-01-01 cst--2010-02-02 01:01:01 cst > str(as.period(span)) formal class 'period' [package "lubridate"] 6 slots ..@ .data : num 1 ..@ year : num 1 ..@ month : num 1 ..@ day : num 1 ..@ hr : num 1 ..@ minute: num 1 r lubridate
Comments
Post a Comment