2013-10-22 50 views
2

我正试图使用​​软件包lubridate计算R中每分钟的费率。下面是I'be尝试:使用lubridate计算每分钟费率

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包

回答

3

首先转换您period到分钟:

count/(period_to_seconds(time)/60) 
# [1] 2.271293 4.237918 0.200000 1.200000 3.000000 
0

它几乎aint't,但:

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