[R] How to cut the time format short

S Ellison S.Ellison at LGCGroup.com
Wed Jan 16 19:35:12 CET 2013


> I have a time format looks like
> 31JAN2002:00:00:00.000
> How could I cut it to
> 31JAN2002

You have several options depending on what form the data are in.

Assuming it is character data, you could use substr:

substr("31JAN2002:00:00:00.000", 1, 9)

or gsub:

gsub("(.{9}).+", "\\1", "31JAN2002:00:00:00.000")

or you could convert to date and reprint:
d <- as.Date("31JAN2002:00:00:00.000", format="%d%b%Y")
format(d, "%d%b%Y") 
	#albeit with lowecase month

All of those should work on vectors.
Some will work on factors but to be safe, wrap the factors in as.character() 

S Ellison

This email and any attachments are confidential. Any use...{{dropped:8}}

More information about the R-help mailing list