[R] Keep date, strip time?
Mark Knecht
markknecht at gmail.com
Tue Feb 8 20:40:41 CET 2011
On Tue, Feb 8, 2011 at 11:29 AM, Phil Spector <spector at stat.berkeley.edu> wrote:
> Mark -
> Here's a few possibilites:
>
>> dts = c('6/10/2009 10:04:00 AM','6/15/2009 9:47:00 AM','6/15/2009 9:47:00
>> AM')
>> as.Date(sapply(strsplit(dts,' '),'[',1),'%m/%d/%Y')
>
> [1] "2009-06-10" "2009-06-15" "2009-06-15"
>>
>> as.Date(sub('(\\d+/\\d+/\\d+) .*','\\1',dts),'%m/%d/%Y')
>
> [1] "2009-06-10" "2009-06-15" "2009-06-15"
>>
>> as.Date(sub('\\d+:\\d+:\\d+ [AP]M','',dts),'%m/%d/%Y')
>
> [1] "2009-06-10" "2009-06-15" "2009-06-15"
>>
>> as.Date(as.POSIXct(dts,format='%m/%d/%Y %H:%M:%S %p'))
>
> [1] "2009-06-10" "2009-06-15" "2009-06-15"
>
> - Phil Spector
> Statistical Computing Facility
> Department of Statistics
> UC Berkeley
> spector at stat.berkeley.edu
>
>
Thanks for a lot of good ideas!
Cheers,
Mark
More information about the R-help
mailing list