我有一个处理包中period
对象的函数lubridate
。minute()
具体来说,在 apply-ie: 中使用 minute 时,我在使用该函数时遇到了错误apply(data, 2, minute)
。
我编写的函数是mean, sd, etc
在将句点对象转换为数字(秒数)后与它们一起使用的包装器。这是代码:
conv_ms <- function(x, func= "mean", na.rm=TRUE, ...) {
# extract min/sec, convert to 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=":")))
}
和执行。当我执行该函数时,我使用minute()
. 但是但是minute
在apply
框架之外可以正常运行。
# run above function -- error message and traceback
conv_ms(men_g[, c(10:11)], na.rm=T)
Error in as.POSIXlt.default(x, tz = tz(x)) :
do not know how to convert 'x' to class “POSIXlt”
5 stop(gettextf("do not know how to convert '%s' to 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 the error -- show error message
# appears to not work within apply
apply(men_g[, c(10:11)], 2, minute)
Error in as.POSIXlt.numeric(x, tz = tz(x)) : 'origin' must be supplied
# but minute works on an 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
数据——编辑为使用 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), hour = 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
), minute = 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", package = "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), hour = 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), minute = 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", package = "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))