[英]R programming help in calculating averages at different intervals
我有一組數據在運行時引用我的時間和距離...所以我有2列與時間和距離有關。 可以說我跑了,總共3000m。 我想要的是我以30秒為間隔行駛的平均距離...因此,我希望我從0-30 s,30 -60 s等行駛的平均距離...
我做了以下代碼:
tapply(data$Distance,cut(data$Time,pretty(range(data$Time),high.u.bias=0.1)),mean)
但這給了我200秒間隔的平均值...我該如何改變呢?
你的剪切語句應該像
# cutting every 30 seconds, starting at 0
# and going up to 30 seconds more than max of Times
cut(dat$Times, breaks=seq(0, max(dat$Times)+30, 30))
# if your time is in minutes, replace 30 with 0.5
# Then you can assign it into your data frame if you'd like,
# but not necessary
cuts <- cut(dat$Times, breaks=seq(0, max(dat$Times)+30, 30))
by(dat$Dist, cuts, mean)
我假設dat
是您的數據幀,而Dist
是您想要平均的向量,而Times
是,那么……您明白了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.