在另一个数据框中匹配日期间隔

时间:2019-03-03 13:48:42

标签: r dataframe

我正在汇总一些要在R中绘制的数据。我想以7天的时间间隔显示数据,x轴为开始的一周。

摘要数据样本:

structure(list(Date = structure(c(17843, 17843, 17844, 17846, 
                              17846, 17847, 17847, 17847, 17847, 17848, 17848, 17871, 17871, 
                              17871, 17871, 17872, 17872, 17873, 17873, 17873, 17873, 17873, 
                              17944, 17945, 17945, 17945, 17945, 17945, 17945, 17945, 17945, 
                              17945, 17945), class = "Date"), Source = df <- structure(c(1L, 1L, 
                                                                                   2L, 1L, 4L, 1L, 3L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 
                                                                                   1L, 1L, 1L, 2L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("direct", 
                                                                                                                                                           "google organic", "other organic search", "PR_referral"), class = "factor"), 
           Revenue = c(1897, 999, 2077.23, 1023.73, 1048, 1897, 949, 
                       949, 999, 849.15, 2077.23, 799.2, 819.18, 865.13, 819.18, 
                       1517.6, 898.2, 2446, 239.2, 867.13, 1517.6, 869.13, 799.2, 
                       853.1, 799.2, 799.2, 799.2, 799.2, 799.2, 799.2, 799.2, 1652.3, 
                       1083.25)), row.names = c(NA, -33L), class = "data.frame")

使用seq内的min以及maxdf$Date日期,我创建了一个7天间隔的数据框:

date_interval <- structure(list(week_beginning = structure(c(17827, 17834, 17841, 
                                                         17848, 17855, 17862, 17869, 17876, 17883, 17890, 17897, 17904, 
                                                         17911, 17918, 17925, 17932, 17939, 17946, 17953), class = "Date"), 
                            week_ending = structure(c(17833, 17840, 17847, 17854, 17861, 
                                                      17868, 17875, 17882, 17889, 17896, 17903, 17910, 17917, 17924, 
                                                      17931, 17938, 17945, 17952, NA), class = "Date")), class = c("tbl_df", 
                                                                                                                   "tbl", "data.frame"), row.names = c(NA, -19L))

我要实现的目的是为df的每次观察在date_interval数据框中引用一列,这是从df$Date数据框中引用的开始的一周。

我认为这可能涉及使用which,但无法使其正常工作,一个不起作用的示例是:

converters %>%
  mutate(test = which(Date >= date_interval$week_beginning & Date <= date_interval$week_ending))

1 个答案:

答案 0 :(得分:1)

据我了解,您想创建一个列,该列的日期由Date列指定。如果是这样,您可以只使用lubridate::floor_date()函数,即

converters %>%
  mutate(week_beginning = lubridate::floor_date(Date, unit = 'weeks'))