Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R lubridate-使用“应用”和“分钟”时出错_R_Lubridate - Fatal编程技术网

R lubridate-使用“应用”和“分钟”时出错

R lubridate-使用“应用”和“分钟”时出错,r,lubridate,R,Lubridate,我有一个函数正在处理lubridate包中的period对象。具体地说,我使用minute()函数时出错,而在apply内部使用minute,即:apply(data,2,minute) 我编写的函数是一个包装器,在将周期对象转换为数字(秒数)后,使用mean、sd等。代码如下: conv_ms <- function(x, func= "mean", na.rm=TRUE, ...) { # extract min/sec, convert to numeric m <-

我有一个函数正在处理
lubridate
包中的
period
对象。具体地说,我使用
minute()
函数时出错,而在apply内部使用minute,即:
apply(data,2,minute)

我编写的函数是一个包装器,在将周期对象转换为数字(秒数)后,使用
mean、sd等
。代码如下:

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=":")))
}
数据——编辑以使用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))

与其粘贴数据输出,不如考虑使用
dput
制作一个可复制的示例。我确实考虑过。我认为用这种方式工作更容易。不?很高兴改变它。我还可以将数据放在GitHub上。@Thomas-编辑为使用
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))