# EXAMPLES OF USING chron() and as.Date() # -------------------------------------------- library(chron) library(Hmisc) # CREATING A FILE # -------------------------------------------- outFile <- file("tmp.txt", open="wt") cat("subject, rd, bd, regist.time, weight\n", file=outFile) cat("\"100000\", \"02-28-2004\", \"2282003\", \"12:10:01\", \"13\"\n", file=outFile) cat("\"100000\", \"06-15-2004\", \"2282003\", \"12:10:01\", \"13.5\"\n", file=outFile) cat("\"222000\", \"11-05-2004\", \"11052003\", \"05:45:05\", \"15\"\n", file=outFile) cat("\"222000\", \"11-05-2004\", \"11052003\", \"05:45:05\", \"14.1\"\n", file=outFile) cat("\"300033\", \"08-17-2001\", \"8172000\", \"18:33:31\", \"18\"\n", file=outFile) cat("\"300033\", \"\", \"8172000\", \"18:33:31\", \"18.7\"\n", file=outFile) close(outFile) # CREATING CHRON OBJECTS # -------------------------------------------- datestr = c("02-28-2004","11-05-2004","08-17-2001",NA) timestr = c("12:10:01","05:45:05","18:33:31","18:33:31") date = chron(dates=datestr, format="m-d-Y") time = chron(times=timestr, format="h:m:s") datetime = chron(dates=date, times=time) # MINUS PLUS < == # -------------------------------------------- datetime1 - datetime date1 = date + c(40, 91, 2, 30) datetime1 = chron(dates=date1, times=time) date1>date datetime1==datetime # DIFF SORT # -------------------------------------------- diff(datetime) sort(datetime) # MINIMUM # -------------------------------------------- min(date) min(date, na.rm=TRUE) #useful operation: find an earliest date for a certain subject data = csv.get("tmp.txt") data$rd = as.character(data$rd) data$rd = dates(data$rd, format="m-d-Y", out.format="year-m-d") data t = tapply(data$rd, data$subject, min, na.rm=TRUE) t class(t); class(t[1]) #unfortunately have to work on restoring the "dates" class ct = chron(dates=t, out.format="year-m-d") ct # AS CHARACTER # -------------------------------------------- datetime date time as.character(datetime) as.character(date) as.character(time) # AS NUMERIC # -------------------------------------------- as.numeric(datetime) as.numeric(date) as.numeric(time) # IS NA # -------------------------------------------- is.na(datetime) # SUMMARY # -------------------------------------------- summary(datetime) summary(date) summary(time) # PLOT & LINES # -------------------------------------------- plot(date, c(1,2,2.3,2.9)) lines(date, c(1,2,2.3,2.9)) # DAYS MONTHS QUATERS YEARS WEEKS WEEKDAYS # -------------------------------------------- # for "dates" it works the same way days(datetime) months(datetime) quarters(datetime) years(datetime) weekdays(datetime) # HOURS MINUTES SECONDS # -------------------------------------------- hours(datetime) hours(time) hours(date) minutes(datetime) seconds(datetime) # DATE CLASS Hmisc WORKS WITH # -------------------------------------------- data = csv.get("tmp.txt", datevars=c("rd"), dateformat="%m-%d-%Y") data$rd class(data$rd) #does not work: #data <- csv.get("tmp.txt", # datevars=c("bd"), # dateformat=c("%m%d%Y")) #does not work: the dates are supposed to have the same format #data <- csv.get("tmp.txt", # datevars=c("rd", "bd"), # dateformat=c("%m-%d-%Y", "%m%d%Y")) #to get around this problem: to convert data$db to date: data$bd = as.character(data$bd) data$bd = ifelse(nchar(data$bd)==7, paste("0",data$bd,sep=""), data$bd) data$bd = as.character(strptime(data$bd, "%m%d%Y")) data$bd = as.Date(data$bd) data$bd class(data$bd) # CONVERT from class "dates" "times" to "Date" # -------------------------------------------- data$bd = chron(dates=as.character(data$bd), format="Y-m-d", out.format="year-mon-day") data$bd # CONVERT from class "Date" to "dates" "times" # -------------------------------------------- data$bd = as.Date(data$bd) data$bd # NOTE: # ---------------------------------------------- #1. format string is different in chron() and as.Date() #2. not all functions defined for chron objects are covered here #3. sasxport.get() (from Hmisc) uses both as.Date() and chron() # it probably depends on the the SAS format. # BONUS: (a little bit heavy bonus - only if you really need it) # ---------------------------------------------- # how to find out a weight measured at the earliest date #varMin <- function(data, arg, var) #{ # argMin = min(data[,arg], na.rm=TRUE) # varMin = data[!is.na(data[,var]),var][data[,arg]==argMin] # return(ifelse(length(varMin)>1, mean(varMin, na.rm=TRUE), varMin)) #} #x = with(data, cbind(subject, rd, weight)) #t = mApply(x, x[,"subject"], FUN=varMin, arg="rd", var="weight") #newD = data.frame(subject=as.numeric(names(t)), startweight=t) # REMOVING TEMPORARY FILE (platform specific command) # --------------------------------------------------- if (.Platform$OS.type == "unix"){ system(paste("rm", tmpFile)) }else{ if (.Platform$OS.type == "windows"){ shell(paste("del", tmpFile)) } }