【问题标题】:R programming help in calculating averages at different intervalsR编程有助于计算不同间隔的平均值
【发布时间】:2013-02-19 15:27:24
【问题描述】:

我有一组数据在我跑步时引用我的时间和距离...所以我有 2 列与时间和距离有关。可以说我跑了,总共3000m。我想要的是我以 30 秒为间隔行驶的平均距离......因此我想要我从 0-30 秒、30 -60 秒等的平均距离......

我做了以下代码:

tapply(data$Distance,cut(data$Time,pretty(range(data$Time),high.u.bias=0.1)),mean)

但这给了我 200 秒间隔的平均值...我该如何更改?

【问题讨论】:

  • 请向我们展示您的数据(或最好使用dput 粘贴您的数据)以供使用。
  • Cute.. 你应该编辑你之前的问题。 stackoverflow.com/questions/14949819/…
  • @NathanG Aw man,我也在努力解决这个问题......
  • @Blake43 如果我不得不猜测,我猜你应该用seq(0, max(data$time), 200) 替换pretty(range(data$Time),high.u.bias=0.1)。尝试使用 cut(data$Time, c(seq(0, max(data$Time), 30), Inf)) 之类的东西代替 cut
  • @sebastian-c 对不起,我一开始以为是同一个问题。你也给出了一个令人印象深刻的答案。

标签: r range average intervals


【解决方案1】:

你的 cut 语句应该类似于

# 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 是,嗯......你明白了。

【讨论】:

  • 感谢这个 ricardo...我想知道是否可以绘制这些数据...比如中断与削减(这是平均值)??
  • 你可以绘制任何你想要的东西;)你可能想把它作为自己的问题发布
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-08
  • 1970-01-01
  • 2018-09-04
  • 1970-01-01
  • 2014-06-26
相关资源
最近更新 更多