From owner-r-help@stat.math.ethz.ch Fri Apr 7 14:03:21 2000 Return-Path: Received: from stat.math.ethz.ch (majordom@hypatia.ethz.ch [129.132.58.23]) by t2.mscf.uky.edu (8.9.3/8.8.7) with ESMTP id OAA08818 for ; Fri, 7 Apr 2000 14:03:20 -0400 Received: by stat.math.ethz.ch (8.9.1/8.9.1) id TAA07862 for r-help-gang-use; Fri, 7 Apr 2000 19:30:37 +0200 (MET DST) Received: (from daemon@localhost) by stat.math.ethz.ch (8.9.1/8.9.1) id TAA07853 for ; Fri, 7 Apr 2000 19:30:32 +0200 (MET DST) Received: from sv1.rsl.psw.fs.fed.us(170.144.8.50) via SMTP by hypatia, id smtpdAAAa001ud; Fri Apr 7 19:30:29 2000 Received: from axe.humboldt.edu (pcwshed5.rsl.psw.fs.fed.us [170.144.8.36]) by sv1.rsl.psw.fs.fed.us (AIX4.3/UCB 8.8.8/8.7) with ESMTP id KAA78018; Fri, 7 Apr 2000 10:30:15 -0700 Message-ID: <38EE1B81.51852224@axe.humboldt.edu> Date: Fri, 07 Apr 2000 10:31:45 -0700 From: Jack Lewis Organization: Redwood Sciences Laboratory, PSW Research Station X-Mailer: Mozilla 4.61 [en] (Win95; U) X-Accept-Language: en MIME-Version: 1.0 To: LRA@northcoast.com, r-help Subject: Re: [R] Plotting with dates on X axis References: <38EBC28A.2EC5@northcoast.com> <38EBFC2D.7D2A4E16@axe.humboldt.edu> <38EC08D5.2FD4@northcoast.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-r-help@stat.math.ethz.ch Precedence: bulk Status: OR Steve (and anyone who might be interested), Here are four simple functions I use in R to create dates and chron objects (assumes you have the "chron" library attached): make.chron <- function(date, time, out = "day mon year") { # Makes a "chron object" from a date (yymmdd) and military time # Date and time can be character or numeric vectors # out is the format for the date portion only ymd2date(date, out = out) + mt2msm(time)/1440 } ymd2date <- function(date, out = "day mon year") { # Converts a numeric or character date (yymmdd) to a "dates" object date <- zfill(date, 6) # pad to 6 characters with starting zeros dates(date, format="ymd", out=out, origin=c(1,1,1960)) } mt2msm <- function(time) { # Converts a military time to minutes since midnight if(data.class(time) == "character") time <- as.numeric(time) min <- time %% 100 hour <- time %/% 100 60 * hour + min } zfill <- function(x, n) { # fill with leading zeros to get an n-digit character string nc <- nchar(x) zeros <- paste(rep(0, n), collapse = "") paste(substring(zeros, nchar(x) + 1, n), substring(x, 1, nchar(x)), sep = "") } In addition, although data.frame() and as.data.frame() will not accept chron objects, you can add chron objects to data frames as follows: data$chr _ chron.obj where data is an existing data frame and chron.obj is an existing chron object. When you display the data frame, the chron object will display as a julian integer, UNLESS you mask the function as.matrix.data.frame() with a copy that has one change: Change the following conditional statement: if (length(levels(xj)) > 0 || !(is.numeric(xj) || is.complex(xj))) non.numeric <- TRUE to: if (length(levels(xj)) > 0 || is.dates(xj) || !(is.numeric(xj) || is.complex(xj))) non.numeric <- TRUE With that, the chron object will appear formatted when the data frame is displayed. -- Jack Lewis Redwood Sciences Laboratory 707-825-2929 voice Pacific Southwest Research Station 707-825-2901 fax USDA Forest Service jl7001@axe.humboldt.edu http://www.rsl.psw.fs.fed.us jlewis01@fs.fed.us (alternate) -------------------------------------------- Thought must be divided against itself before it can come to any knowledge of itself. (Aldous Huxley) -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._