【问题标题】:how to replace the value for one timestamp by the mean value of all the observations for this timestamp如何用该时间戳的所有观察值的平均值替换一个时间戳的值
【发布时间】:2020-02-09 13:36:51
【问题描述】:

在处理 R 中的时间序列问题时,我对一个时间戳有多个观察值,如何将一个时间戳的值替换为此时间戳的所有观察值的平均值并删除所有重叠的时间戳行。

例如,我有一个这样的时间序列:

Date={2016-3-1, 2016-4-1, 2016-4-1, 2016-4-1,2016-5-1,2015-5-1, 2016-6-1).
Price={111,122,124,155,142,177,134}

我的预期结果是这样的:

Date={2016-3-1,2016-4-1,2016-5-1,2017-6-1}, 
Price={111,133.67,159.5,134 }
(133.67=mean(122,124,155), 159.5=mean(142,177))

【问题讨论】:

    标签: r time-series outliers imputation forecast


    【解决方案1】:

    这就是你所追求的吗?

    Date=c("2016-3-1", "2016-4-1", "2016-4-1", "2016-4-1","2016-5-1","2015-5-1", "2017-6-1")
    Price=c(111,122,124,155,142,177,134)
    
    library(dplyr)
    library(lubridate)
    df <- data.frame(Date = ymd(Date), Price = Price)
    df %>% 
      group_by(ymd(Date)) %>%
      summarise(mean = sprintf("%0.2f",mean(Price, na.rm = TRUE)))
    
    # # A tibble: 5 x 2
    #     `ymd(Date)` mean  
    #     <date>      <chr> 
    # 1 2015-05-01  177.00
    # 2 2016-03-01  111.00
    # 3 2016-04-01  133.67
    # 4 2016-05-01  142.00
    # 5 2017-06-01  134.00
    

    如果你想要baseR 这也可以:

    print(aggregate(list(Mean=df$Price), list(Date = df$Date), mean), digits = 5)
    
    #       Date   Mean
    # 1 2015-05-01 177.00
    # 2 2016-03-01 111.00
    # 3 2016-04-01 133.67
    # 4 2016-05-01 142.00
    # 5 2017-06-01 134.00
    

    【讨论】:

    • 是的。非常感谢!
    猜你喜欢
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    • 2017-06-03
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    • 2018-09-23
    • 1970-01-01
    相关资源
    最近更新 更多