Frage

I'm trying to calculate a rate per minute in R using the package lubridate. Here's what I'be tried:

library(lubridate)

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

count/time

This throws the error:

Error in count/time : Cannot divide numeric by period

How can I calculate rate per minute? I'm specifically after a solution using lubridate package

War es hilfreich?

Lösung

First convert your period to minutes :

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

Andere Tipps

It aint't pretty, but:

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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top