[R] A solution for question about formatting Dates

Erin Hodgess erinm.hodgess at gmail.com
Wed Jun 27 06:57:04 CEST 2012


Hello again:

Here is a solution to the dates without leading zeros:

pou1 <- function(x) {
     #Note:  x is a data frame
     #Assume that Column 1 has the date
     #Column 2 has station
     #Column 3 has min	
     #Column 4 has max
     library(stringr)
     w <- character(length=nrow(x))
     z <- str_split(x[,1],"/")
     for(i in 1:nrow(x)) {
     	   u <-  str_pad(z[[i]][1:3],width=2,pad="0")
	   w[i] <- paste(u,sep="",collapse="/")
	
	   }
     a <- as.Date(w,"%m/%d/%Y")

This is not particularly elegant, but it does the trick.


Thanks,
Erin


-- 
Erin Hodgess
Associate Professor
Department of Computer and Mathematical Sciences
University of Houston - Downtown
mailto: erinm.hodgess at gmail.com



More information about the R-help mailing list