2

パッケージlubridateを使用して、Rで1分あたりの速度を計算しようとしています。これが私が試したことです:

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

1 分あたりのレートを計算するにはどうすればよいですか? 私は特に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 に答える