使用最大日期过滤数据并提取最近3个月的数据

时间:2019-04-02 13:52:08

标签: r filter

我正在尝试过滤日期栏中显示的R w.r.t最大日期中的最近3个月的数据。

我最初在现有数据框中添加了一列,并使用以下表达式过滤数据。我要消除此多余的步骤并直接进行过滤。

last_three_month <- df_1[date_format > max(as.Date(date_format)) %m-% months(4) & 
                         date_format <= max(date_format) , ]

当我使用df_1[MonthYear]并过滤数据时,我会看到类似

的错误
  

“错误:‘最大’对因素没有意义”

数据

MonthYear   Number    Risk 
1/18/2019   101      High AESI
1/18/2019   905 
1/18/2019   909 
1/18/2019   904 
2/18/2019   101      High AESI
2/18/2019   905 
2/18/2019   904 
2/18/2019   909 
2/18/2019   907 
2/18/2019   541      High AESI
2/18/2019   908      High AESI
2/18/2019   906      High AESI
2/18/2019   046 
2/18/2019   018      High AESI
2/18/2019   019 
2/18/2019   002      High AESI
3/18/2019   904 
3/18/2019   907 
3/18/2019   905 

代码

library(dplyr)
library(tibble)
library(reshape)

Input <- read.csv("C:/Users/Documents/Exports/HR.csv")
Output <- Input #%>% filter(Year == 2019)
df_output <- as.data.frame(Output)
date_format <- as.Date(paste("01-", df_output$Month.Year, sep = ""), 
                       format = "%d-%b-%y")
df_1 <- cbind(df_output, date_format)

last_three_month <- df_1[date_format > max(as.Date(date_format)) %m-% months(4) &
                         date_format <= max(date_format) , ]

我实际上是在尝试直接在数据框中过滤数据,而不是增加一列并实现它。能请你指教-谢谢

2 个答案:

答案 0 :(得分:1)

read.csv默认将字符串(例如"1/18/2019")导入为“ factor”列。此分类数据类不是“日期”。而是使用readr::read_csv,它可能会检测到您的日期列是一个日期,然后应允许您的代码按预期运行。如果将RStudio导入向导与 reader 选项一起使用,则可以手动选择列类型,然后将为您创建相应的代码。

答案 1 :(得分:1)

请考虑使用seq(),如@G所示。 Grothendieck的answer here从max MonthYear 最大值中检索三个月前的日期。

数据 (某些日期已转换为2018年,以演示三个月的过滤条件)

txt <- 'MonthYear   Number    Risk 
"1/18/2018"   101      "High AESI"
"1/18/2018"   905      NA
"1/18/2019"   909      NA
"1/18/2019"   904      NA 
"2/18/2018"   101      "High AESI"
"2/18/2018"   905      NA 
"2/18/2019"   904      NA 
"2/18/2019"   909      NA 
"2/18/2019"   907      NA 
"2/18/2019"   541      "High AESI"
"2/18/2019"   908      "High AESI"
"2/18/2019"   906      "High AESI"
"2/18/2019"   046      NA 
"2/18/2019"   018      "High AESI"
"2/18/2019"   019      NA 
"2/18/2019"   002      "High AESI"
"3/18/2018"   904      NA 
"3/18/2019"   907      NA 
"3/18/2019"   905      NA '

Input <- read.table(text=txt, header=TRUE)

代码

# CONVERT TO DATE
Input$MonthYear <- as.Date(Input$MonthYear, format = "%m/%d/%Y")

# SUBSET DATA
last_three_month_df <- subset(Input, MonthYear > seq(as.Date(max(MonthYear)), length=2, by="-3 months")[2] &
                                     MonthYear <= max(MonthYear))    

last_three_month_df 
#     MonthYear Number      Risk
# 3  2019-01-18    909      <NA>
# 4  2019-01-18    904      <NA>
# 7  2019-02-18    904      <NA>
# 8  2019-02-18    909      <NA>
# 9  2019-02-18    907      <NA>
# 10 2019-02-18    541 High AESI
# 11 2019-02-18    908 High AESI
# 12 2019-02-18    906 High AESI
# 13 2019-02-18     46      <NA>
# 14 2019-02-18     18 High AESI
# 15 2019-02-18     19      <NA>
# 16 2019-02-18      2 High AESI
# 18 2019-03-18    907      <NA>
# 19 2019-03-18    905      <NA>