如何根据日期R找到该月的最后一天

时间:2016-10-12 23:37:47

标签: r date

我想创建一个标记值是否是该月的最后一天的列。考虑我的初始数据表和代码:

      Date       Companyvar variable value DOW Month EndOfMonth
 1: 1992-07-31     VAHXRI       MV    NA Friday   Jul          0
 2: 1992-07-31     VAHXTD       MV    NA Friday   Jul          0
 3: 1992-07-31     VAHXND       MV    NA Friday   Jul          0
 4: 1992-07-31      ASXMV       MV    NA Friday   Jul          0
 5: 1992-07-31      ASXRI       MV    NA Friday   Jul          1
 6: 1992-08-03     AIZXMV       MV    NA Monday   Aug          0
 7: 1992-08-03     AIZXRI       MV    NA Monday   Aug          0
 8: 1992-08-03     AIZXTD       MV    NA Monday   Aug          0
 9: 1992-08-03     AIZXND       MV    NA Monday   Aug          0
10: 1992-08-03     AQZXMV       MV    NA Monday   Aug          0

sapply(trans, class) 
       Date  Companyvar    variable       value         DOW       Month  EndOfMonth 
     "Date"    "factor"    "factor"   "numeric" "character" "character"   "numeric" 

我创造了" EndOfMonth"专栏:

trans <- trans[, EndOfMonth := ifelse(shift(Month, type = "lead", n = 1) != Month, 1, 0)]

但是,正如您所看到的,我在7月31日有多个日期,但只有最后一个被标记为&#34; EndOfMonth&#34;。据我所知,我的代码仅在Month列中有变化时才会查找,根据定义,它只会从7月到8月进行单一变化。

我想知道是否有办法让7月31日的所有5个日期都被标记为&#34; EndOfMonth&#34;而不仅仅是最后一个。

即所需的输出是:

          Date Companyvar variable value    DOW Month EndOfMonth
 1: 1992-07-31     VAHXRI       MV    NA Friday   Jul          1
 2: 1992-07-31     VAHXTD       MV    NA Friday   Jul          1
 3: 1992-07-31     VAHXND       MV    NA Friday   Jul          1
 4: 1992-07-31      ASXMV       MV    NA Friday   Jul          1
 5: 1992-07-31      ASXRI       MV    NA Friday   Jul          1
 6: 1992-08-03     AIZXMV       MV    NA Monday   Aug          0
 7: 1992-08-03     AIZXRI       MV    NA Monday   Aug          0
 8: 1992-08-03     AIZXTD       MV    NA Monday   Aug          0
 9: 1992-08-03     AIZXND       MV    NA Monday   Aug          0
10: 1992-08-03     AQZXMV       MV    NA Monday   Aug          0

前5行被标记为&#34; EndOfMonth&#34;。

这里有30行数据:

 structure(list(Date = structure(c(8247, 8247, 8247, 8247, 8247, 
8247, 8247, 8247, 8247, 8247, 8247, 8247, 8247, 8247, 8247, 8247, 
8247, 8247, 8247, 8247, 8247, 8250, 8250, 8250, 8250, 8250, 8250, 
8250, 8250, 8250, 8250), class = "Date"), Companyvar = structure(c(50L, 
51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 
64L, 65L, 66L, 67L, 68L, 69L, 70L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L), .Label = c("AIZXMV", "AIZXRI", "AIZXTD", "AIZXND", 
"AQZXMV", "AQZXRI", "AQZXTD", "AQZXND", "AIAYMV", "AIAYRI", "AIAYTD", 
"AIAYND", "AZJXMV", "AZJXRI", "AZJXTD", "AZJXND", "CHRXMV", "CHRXRI", 
"CHRXTD", "CHRXND", "CLXXMV", "CLXXRI", "CLXXTD", "CLXXND", "KSCXMV", 
"KSCXRI", "KSCXTD", "KSCXND", "LAUXMV", "LAUXRI", "LAUXTD", "LAUXND", 
"MQAXMV", "MQAXRI", "MQAXTD", "MQAXND", "MRMXMV", "MRMXRI", "MRMXTD", 
"MRMXND", "QANXMV", "QANXRI", "QANXTD", "QANXND", "QUBXMV", "QUBXRI", 
"QUBXTD", "QUBXND", "REXXMV", "REXXRI", "REXXTD", "REXXND", "SYDXMV", 
"SYDXRI", "SYDXTD", "SYDXND", "TTIXMV", "TTIXRI", "TTIXTD", "TTIXND", 
"TCLXMV", "TCLXRI", "TCLXTD", "TCLXND", "VAHXMV", "VAHXRI", "VAHXTD", 
"VAHXND", "ASXMV", "ASXRI"), class = "factor"), variable = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), class = "factor", .Label = c("MV", 
"ND", "RI", "TD")), value = c(NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), 
    DOW = c("Friday", "Friday", "Friday", "Friday", "Friday", 
    "Friday", "Friday", "Friday", "Friday", "Friday", "Friday", 
    "Friday", "Friday", "Friday", "Friday", "Friday", "Friday", 
    "Friday", "Friday", "Friday", "Friday", "Monday", "Monday", 
    "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", 
    "Monday", "Monday"), Month = c("Jul", "Jul", "Jul", "Jul", 
    "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", 
    "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Aug", 
    "Aug", "Aug", "Aug", "Aug", "Aug", "Aug", "Aug", "Aug", "Aug"
    ), EndOfMonth = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("Date", 
"Companyvar", "variable", "value", "DOW", "Month", "EndOfMonth"
), class = c("data.table", "data.frame"), row.names = c(NA, -31L
), .internal.selfref = <pointer: 0x000000000b000788>)

干杯

0 个答案:

没有答案