0

ベクトルに一連の時間値があります。

# Source data in %m:%s"
>intervals <- c("1:30", "1:33", "1:29", "1:31", "1:30")

これらの値を平均化したい場合は、次のstrptime()関数を使用できます。

# Averaging with strptime()
> meantime <- mean(strptime(intervals,"%M:%S"))
> meantime
[1] "2013-11-01 00:01:30 PDT"

しかし、日付とタイムゾーンは必要ありません。そして、その時間値をさらに計算したいときに問題があります。たとえば、これらの時間値は、距離が 400 m のレース トラックに属しているとします。平均速度とペースを計算してみましょう。

> distance <- as.numeric(400)
> distance
[1] 400
> # Let's calculate the average speed:
> distance/meantime
Error in Ops.POSIXt(distance, meantime) : 
  '/' not defined for "POSIXt" objects
> # Let's calculate the average pace:
> meantime / distance
Error in Ops.POSIXt(meantime, distance) : 
  '/' not defined for "POSIXt" objects

質問: これと同様の計算を行うために時間値を処理する簡単でクリーンな方法はありますか?

4

1 に答える 1

0

difftime 関数は、datetime 値の差 (秒単位および GMT に対する相対値) を返します。

inters <- strptime(intervals,"%M:%S")
inters
difftime( inters , Sys.Date())
Time differences in hours
[1] 7.025000 7.025833 7.024722 7.025278 7.025000
attr(,"tzone")
[1] ""
# I appear to be 7 hours off from GMT here on the Left Coast of the US.

期待していたほど簡単ではありませんでした。as.difftime関数を見てみましょう。

intervals <- c("1:30", "1:33", "1:29", "1:31", "1:30")
inters <- as.difftime(intervals,"%M:%S")
inters
#
Time differences in mins
[1] 1.500000 1.550000 1.483333 1.516667 1.500000
attr(,"tzone")
[1] ""

distance=400
>     as.numeric(inters)/ distance
[1] 0.003750000 0.003875000 0.003708333 0.003791667 0.003750000
# Remembering that these are in minutes
于 2013-11-02T02:07:34.607 に答える