【问题标题】:How to select the earliest date in a month from a Date series in R?如何从R中的日期系列中选择一个月中最早的日期?
【发布时间】:2023-03-31 17:02:01
【问题描述】:

我有一个数据库,其中包含具有不同频率(每周、每月、每天)数据的不同指数的值。我希望通过从时间序列中提取月初值来计算月收益。

我尝试使用循环逐月对时间序列进行分区,然后使用 min() 来获取该月的最早日期。但是,我想知道是否有更有效的方法来加快计算速度。

library(data.table)
df<-fread("statistic_date index_value funds_number
           2013-1-1    1000.000            0
           2013-1-4     996.096           21
           2013-1-11    1011.141           21
           2013-1-18    1057.344           21
           2013-1-25    1073.376           21
           2013-2-1    1150.479           22
           2013-2-8    1150.288           19
           2013-2-22    1112.993           18
           2013-3-1    1148.826           20
           2013-3-8    1093.515           18
           2013-3-15    1092.352           17
           2013-3-22    1138.346           18
           2013-3-29    1107.440           17
           2013-4-3    1101.897           17
           2013-4-12    1093.344           17")

我希望过滤得到每个月最早日期的行,例如:

2013-1-1    1000.000            0
2013-2-1    1150.479           22
2013-3-1    1148.826           20
2013-4-3    1101.897           17

您的帮助将不胜感激!

【问题讨论】:

    标签: r date filter


    【解决方案1】:

    使用 tidyverse 和 lubridate 包,

    library(lubridate)
    library(tidyverse)
    df %>% mutate(statistic_date = ymd(statistic_date), # convert statistic_date to date format
                  month = month(statistic_date),  #create month and year columns
                  year= year(statistic_date)) %>%
           group_by(month,year) %>% # group by month and year
           arrange(statistic_date) %>% # make sure the df is sorted by date
           filter(row_number()==1) # select first row within each group
    
    
    
    # A tibble: 4 x 5
    # Groups:   month, year [4]
    #  statistic_date index_value funds_number month  year
    #  <date>               <dbl>        <int> <dbl> <dbl>
    #1 2013-01-01           1000             0     1  2013
    #2 2013-02-01           1150.           22     2  2013
    #3 2013-03-01           1149.           20     3  2013
    #4 2013-04-03           1102.           17     4  2013
    

    【讨论】:

      【解决方案2】:

      首先将statistic_date 设为日期:

      df$statistic_date <- as.Date(df$statistic_date)
      

      您可以使用nth_daystatistic_date 中查找每个月的第一天。

      library("datetimeutils")
      dates <- nth_day(df$statistic_date, period = "month", n = "first")
      ## [1] "2013-01-01" "2013-02-01" "2013-03-01" "2013-04-03"
      
      df[statistic_date %in% dates]
      ##    statistic_date index_value funds_number
      ## 1:     2013-01-01    1000.000            0
      ## 2:     2013-02-01    1150.479           22
      ## 3:     2013-03-01    1148.826           20
      ## 4:     2013-04-03    1101.897           17
      

      【讨论】:

        猜你喜欢
        • 2016-12-30
        • 1970-01-01
        • 1970-01-01
        • 2020-01-18
        • 2015-07-11
        • 1970-01-01
        • 2018-02-16
        • 1970-01-01
        • 2020-04-07
        相关资源
        最近更新 更多