我正在尝试使用包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包的解决方案
答案 0 :(得分:3)
首先将period
转换为分钟:
count/(period_to_seconds(time)/60)
# [1] 2.271293 4.237918 0.200000 1.200000 3.000000
答案 1 :(得分: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