2

我正在尝试使用包 lubridate 计算 R 中的每分钟速率。这是我尝试过的:

library(lubridate)

time <- ms(c("5M 17S", "4M 29S", "5M 0S",  "5M 0S",  "5M 20S"))
count <- sample(1:20, 5)

count/time

这会引发错误:

Error in count/time : Cannot divide numeric by period

如何计算每分钟的速率?我特别关注使用 lubridate 包的解决方案

4

2 回答 2

3

首先将您的转换period为分钟:

count/(period_to_seconds(time)/60)
# [1] 2.271293 4.237918 0.200000 1.200000 3.000000
于 2013-10-22T20:44:24.870 回答
0

它不漂亮,但是:

time <- c("5m 17s", "4m 29s", "5m0s",  "5m0s",  "5m20s")
count <- sample(1:20, 5)

countTime <- function(count, time){
  require(lubridate)
  time <- ms(time)
  timeConvert <- period_to_seconds(time)/60
  countTime <- count/timeConvert
  return(countTime)
}

countTime(count, time)

[1] 3.028391 1.338290 1.800000 3.600000 2.437500
于 2013-10-22T21:28:27.410 回答