如何计算r

时间:2017-11-12 13:32:24

标签: r date time-series mean

我的data.frame包含数字值(Rate),日期(TrdDate) - 格式为1990-01-29。我创建了一个与每个日期的工作日相匹配的列,我想计算每天的平均值。

      TrdDate Rate   weekday 
1. 1990-02-20  120    monday
2. 1990-03-20  100    monday
3. 1990-04-21  100   Tuesday
4. 1990-05-01  100  Thursday
5. 1991-01-01  150  Thursday

所需的输出是:

 weekday Rate
  Monday  110
 Tuesday  100
Thursday  125

2 个答案:

答案 0 :(得分:1)

DF <- read.table(text="TrdDate Rate   weekday 
 1990-02-20  120    monday
 1990-03-20  100    monday
 1990-04-21  100   Tuesday
 1990-05-01  100  Thursday
 1991-01-01  150  Thursday",strin=T,h=T)

aggregate(Rate ~ weekday ,DF,mean)
#    weekday Rate
# 1   monday  110
# 2 Thursday  125
# 3  Tuesday  100

答案 1 :(得分:0)

如果您的数据框为df,则可以

library(dplyr)

group_by(df, weekday) %>%
  summarise(Rate = mean(Rate))