R中的日期比较

时间:2018-08-01 18:03:00

标签: r

R学习者在这里。我正在尝试使用R来比较日期,并基于日期介于何时之间应用值。

例如,如果Acq_Dt早于1984年1月10日,则Cap_Threshold应当为1000,如果是1984年10月1日至1991年9月30日之间,则应为5000,依此类推。一切都被评估为5000。任何帮助将不胜感激。

temp1$Acq_Dt <-as.Date(temp1$Acq_Dt,format="%m/%d/%Y") 
temp1$CapThreshold <- if (temp1$Acq_Dt < "1984-10-01") {         
   1000
  } else if (temp1$Acq_Dt >= "1984-10-01" & temp1$Acq_Dt <= "1991-09-30")  {
   5000
  } else if (temp1$Acq_Dt >= "1991-10-01" & temp1$Acq_Dt <= "1993-09-30") {
   15000
  } else if (temp1$Acq_Dt >= "1993-10-01" & temp1$Acq_Dt <= "1994-09-30")  {
    25000
  } else if (temp1$Acq_Dt >= "1994-10-01" & temp1$Acq_Dt <= "1995-09-30") {
    50000
  } else if (temp1$Acq_Dt >= "1995-10-01" & temp1$Acq_Dt <= "2013-09-30") {
    100000
  } else if (temp1$Acq_Dt >= "2013-10-01") {
    1000000
} else { 
  0
}

3 个答案:

答案 0 :(得分:1)

一种替代方法是使用cut并强制转换为数字:

set.seed(11)
temp1 <- data.frame(Acq_Dt = sample(seq(as.Date('1984-09-01'), as.Date('2013-11-01'), by = 'day'), 100))

breaks <- as.Date(c("1500-10-01", "1984-10-01", "1991-10-01", "1993-10-01", 
                    "1994-10-01", "1995-10-01", "2013-10-01", "2020-10-01"))

thresholds <- c(1000, 5000, 15000, 25000, 50000, 100000, 1000000)

temp1$Capthreshold <- as.numeric(as.character(cut(temp1$Acq_Dt, 
                                                  breaks = breaks,
                                                  labels = thresholds,
                                                  include.lowest = TRUE)))

结果:

        Acq_Dt Capthreshold
1   1992-10-02        15000
2   1984-09-06         1000
3   1999-07-24       100000
4   1985-01-28         5000
5   1986-07-21         5000
6   2012-07-04       100000
7   1987-03-11         5000
8   1993-02-13        15000
9   2010-05-03       100000
10  1988-04-04         5000
11  1989-10-08         5000
12  1997-07-05       100000
13  2011-02-06       100000
14  2009-06-17       100000
15  2006-01-18       100000
16  2001-05-18       100000
17  1998-09-13       100000
18  1994-04-18        25000
19  1989-04-04         5000
20  1998-08-25       100000
...

答案 1 :(得分:0)

temp1 <-as.Date("11/10/1991",format="%m/%d/%Y")

Barrier1<-as.Date("1984-10-01",format="%Y-%m-%d")
Barrier2<-as.Date("1991-09-30",format="%Y-%m-%d")
Barrier3<-as.Date("1993-09-30",format="%Y-%m-%d")
Barrier4<-as.Date("1994-09-30",format="%Y-%m-%d")
Barrier5<-as.Date("1995-09-30",format="%Y-%m-%d")
Barrier6<-as.Date("2013-09-30",format="%Y-%m-%d")
Threshold <- if (temp1 < Barrier1) {         
   1000
  } else if (temp1 > Barrier1 & temp1 <= Barrier2)  {
   5000
  } else if (temp1 > Barrier2 & temp1 <= Barrier3) {
   15000
  } else if (temp1 > Barrier3 & temp1 <= Barrier4)  {
    25000
  } else if (temp1 > Barrier4 & temp1 <= Barrier5) {
    50000
  } else if (temp1 > Barrier5 & temp1 <= Barrier6) {
    100000
  } else if (temp1> Barrier6) {
    1000000
} else { 
  0
}

基本上表明您的数据必须存在一些问题,因为此代码有效。

答案 2 :(得分:0)

  library(dplyr)

  SB$Cap_Threshold<- case_when(
  SB$RECEIPT_DATE < "1984-10-01" ~ 1000,
  SB$RECEIPT_DATE >= "1984-10-01" & SB$RECEIPT_DATE <="1991-09-30" ~ 5000,
  SB$RECEIPT_DATE >= "1991-10-01" & SB$RECEIPT_DATE <="1993-09-30" ~ 15000,
  SB$RECEIPT_DATE >= "1993-10-01" & SB$RECEIPT_DATE <="1994-09-30" ~ 25000,
  SB$RECEIPT_DATE >= "1994-10-01" & SB$RECEIPT_DATE <="1995-09-30" ~ 50000,
  SB$RECEIPT_DATE >= "1995-10-01" & SB$RECEIPT_DATE <="2013-09-30" ~ 100000,
  SB$RECEIPT_DATE >= "2013-10-01"  ~ 1000000,
  TRUE ~ 999999999999999999
  )