1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
as.date <- function(x, order = "mdy", ...) {
if (inherits(x, "date")) x
else if (inherits(x,"POSIXt")){
rval<-difftime(x,ISOdate(1960,1,1),units="days")
structure(rval,class="date")
}
else if (is.character(x)) {
order.vec <-
switch(order,
"ymd" = c(1, 2, 3),
"ydm" = c(1, 3, 2),
"mdy" = c(2, 3, 1),
"myd" = c(2, 1, 3),
"dym" = c(3, 1, 2),
"dmy" = c(3, 2, 1),
stop("Invalid value for `order' option"))
nn <- length(x)
temp <- .C("char_date",
as.integer(nn),
as.integer(order.vec),
as.character(x),
month =integer(nn),
day = integer(nn),
year = integer(nn),
PACKAGE = "survival")
month <- ifelse(temp$month < 1 | temp$month > 12, NA, temp$month)
day <- ifelse(temp$day == 0, NA, temp$day)
year <- ifelse(temp$year == 0, NA, temp$year)
temp <- mdy.date(month, day, year, ...)
}
else if (is.numeric(x)) {
temp <- floor(x)
attr(temp, "class") <- "date"
}
else stop("Cannot coerce to date format")
temp
}
is.date <- function(x)
inherits(x, "date")
Ops.date <- function(e1, e2) {
## Certain operation yield a date, others just give a number. In
## order to make plotting functions work well, we end up allowing
## most all numeric operations.
if (missing(e2))
stop("Unary operations not meaningful for dates")
if (.Generic == "&" || .Generic== "|")
stop(paste("\`", .Generic, "' not meaningful for dates",
sep = ""))
class(e1) <- NULL
class(e2) <- NULL
if (.Generic == "-") {
if (.Method[2] == "" ) {
## subtract a constant from a date
e1 <- as.integer(e1 - e2)
class(e1) <- "date"
e1
}
else if ((.Method[1] == "Ops.date" && .Method[2] == "Ops.date") ||
(.Method[1] == ""))
e1 - e2
else
## date - factor should fail
stop("Invalid operation for dates")
}
else if (.Generic == "+") {
if (.Method[1] == "" || .Method[2]=="") {
## add constant to a date
e1 <- as.integer(e1 + e2);
class(e1) <- "date"
e1
}
else e1 + e2
}
else get(.Generic)(e1, e2)
}
Math.date <- function(...)
stop("Invalid operation on dates")
Summary.date <- function (..., na.rm = FALSE) {
ok <- switch(.Generic, min = , max = , range = TRUE, FALSE)
if (!ok)
stop(paste(.Generic, "not defined for dates"))
as.date(NextMethod(.Generic))
}
"[.date" <- function(x, ..., drop = TRUE) {
cl <- class(x)
class(x) <- NULL
x <- NextMethod("[")
class(x) <- cl
x
}
"[[.date" <- function(x, ..., drop = TRUE) {
cl <- class(x)
class(x) <- NULL
x <- NextMethod("[[")
class(x) <- cl
x
}
as.character.date <- function(x) {
fun <- options()$print.date
if (is.null(fun))
date.ddmmmyy(x)
else
get(fun)(x)
}
as.data.frame.date <- as.data.frame.vector
as.vector.date <- function(x, mode = "any") {
if (mode == "any" || mode == "character" || mode == "logical" ||
mode == "list")
as.vector(as.character(x), mode)
else as.vector(unclass(x), mode)
}
is.na.date <- function(x) {
NextMethod(.Generic)
}
plot.date <- function(x, y, ..., axes, xaxt, xlab, ylab,
xlim = range(x, na.rm = TRUE),
ylim = range(y, na.rm = TRUE))
{
if(missing(xlab))
xlab <- deparse(substitute(x))
if(missing(ylab))
ylab <- deparse(substitute(y))
class(x) <- NULL # after deparse(substitute())
if(!missing(axes) && !axes) # argument axes works
plot(x, y, ..., axes = axes, xlab = xlab, ylab = ylab,
xlim = xlim, ylim = ylim)
else if(!missing(xaxt))
plot(x, y, ..., xaxt = xaxt, xlab = xlab, ylab = ylab,
xlim = xlim, ylim = ylim)
else {
plot(x, y, ..., xaxt = "n", xlab = xlab, ylab = ylab,
xlim = xlim, ylim = ylim)
x <- c(x[!is.na(x)], xlim) # draws axis completely when
# using xlim
xd <- date.mdy(x)
## get default for n from par("lab")
temp <- pretty(x, n = par("lab")[1])
delta <- temp[2] - temp[1]
if(delta < 1)
temp <- seq(min(x), max(x), 1)
else if(delta > 182) {
temp <- xd$year + (x - mdy.date(1, 1, xd$year))/365
## get default for n from par("lab")
temp <- pretty(temp, n = par("lab")[1])
temp <- mdy.date(1, 1, floor(temp)) + floor((temp %% 1) * 365)
}
axis(1, temp, as.character.date(temp), ...)
}
}
print.date <- function(x, quote, prefix, ...) {
if (missing(quote))
quote <- FALSE
invisible(print(as.character(x), quote = quote))
}
print.date <- function(x, quote, prefix, ...) {
fun <- options()$print.date
if (is.null(fun))
x <- date.ddmmmyy(x)
else
x <- get(fun)(x)
if (missing(quote))
quote <- FALSE
invisible(print(x, quote=quote))
}
summary.date <- function(object, ...) {
y <- as.character(range(object))
names(y) <- c("First ", "Last ")
y
}
mdy.date <- function(month, day, year, nineteen = TRUE, fillday = FALSE,
fillmonth = FALSE) {
## Get the Julian date, but centered a la SAS, i.e., Jan 1 1960 is
## day 0. Algorithm taken from Numerical Recipies.
temp <- any((month != trunc(month)) |
(day != trunc(day)) |
(year != trunc(year)))
if (!is.na(temp) && temp) {
warning("Non integer input values were truncated in mdy.date")
month <- trunc(month)
day <- trunc(day)
year <- trunc(year)
}
if (nineteen)
year <- ifelse(year < 100, year + 1900, year)
## Force input vectors to be the same length, but in a way that
## gives an error if their lengths aren't multiples of each other.
temp <- numeric(length(month + day + year))
month <- month + temp
day <- day + temp
year <- year + temp
if (fillmonth) {
temp <- is.na(month)
month[temp] <- 7
day[temp] <- 1
}
if (fillday) day[is.na(day)] <- 15
month[month < 1 | month > 12] <- NA
day[day < 1] <- NA
year[year == 0] <- NA # there is no year 0
year <- ifelse(year < 0, year + 1, year)
tyear<- ifelse(month > 2, year, year - 1)
tmon <- ifelse(month > 2, month + 1, month + 13)
julian <-
trunc(365.25 * tyear) + trunc(30.6001 * tmon) + day - 715940
## Check for Gregorian calendar changeover on Oct 15, 1582
temp <- trunc(0.01 * tyear)
save <- ifelse(julian >= -137774,
julian + 2 + trunc(.25 * temp) - temp,
julian)
## Check for invalid days (31 Feb, etc.) by calculating the Julian
## date of the first of the next month
year <- ifelse(month == 12, year+1, year)
month<- ifelse(month == 12, 1, month + 1)
day <- 1
tyear<- ifelse(month > 2, year, year - 1)
tmon <- ifelse(month > 2, month + 1, month + 13)
julian <-
trunc(365.25 * tyear) + trunc(30.6001 * tmon) + day - 715940
temp <- trunc(0.01 * tyear)
save2<- ifelse(julian >= -137774,
julian + 2 + trunc(.25 * temp) - temp,
julian)
temp <- as.integer(ifelse(save2 > save, save, NA))
attr(temp, "class") <- "date"
temp
}
date.mdy <- function(sdate, weekday = FALSE) {
## Return the month, day, and year given a julian date
attr(sdate, "class") <- NULL # Stop any propogation of methods
sdate <- sdate + 2436935 # From SAS to Num Recipies base
# point
wday <- as.integer((sdate + 1) %% 7 +1)
temp <- ((sdate - 1867216) -.25) / 36524.25
sdate <- ifelse(sdate >= 2299161,
trunc(sdate+ 1 +temp - trunc(.25 * temp)),
sdate)
jb <- sdate + 1524
jc <- trunc(6680 + ((jb - 2439870) - 122.1) / 365.25)
jd <- trunc(365.25 * jc)
je <- trunc((jb - jd)/ 30.6001)
day <- (jb - jd) - trunc(30.6001 * je)
month <- as.integer(ifelse(je > 13, je - 13, je - 1))
year <- as.integer(ifelse(month > 2, jc - 4716, jc - 4715))
year <- as.integer(ifelse(year <= 0, year - 1, year))
if (weekday)
list(month = month, day = day, year = year, weekday = wday)
else
list(month = month, day = day, year = year)
}
date.ddmmmyy <- function(sdate) {
temp <- date.mdy(sdate)
tyr <- ifelse(floor(temp$year/100) == 19,
temp$year-1900, temp$year)
month <- month.abb[temp$month]
ifelse(is.na(sdate), "NA",
paste(temp$day, month, tyr, sep = ""))
}
date.mmddyy <- function(sdate, sep = "/") {
temp <- date.mdy(sdate)
tyr <- ifelse(floor(temp$year / 100) == 19,
temp$year - 1900, temp$year)
ifelse(is.na(sdate), "NA",
paste(temp$month, temp$day, tyr, sep = sep))
}
date.mmddyyyy <- function(sdate, sep = "/") {
temp <- date.mdy(sdate)
ifelse(is.na(sdate), "NA",
paste(temp$month, temp$day, temp$year, sep = sep))
}
|