[R] Time format issue

David Winsemius dwinsemius at comcast.net
Wed Jul 27 21:40:07 CEST 2016


> On Jul 27, 2016, at 5:03 AM, Ismail SEZEN <sezenismail at gmail.com> wrote:
> 
> 
>>> The time column is factor. I want to convert each time to the following
>>> format:
>>> 
>>> 20111211003000
>>> 
>>> 20111211013000
>>> 
>>> 20111211020000
>>> 
>>> (Year)(month)(date)(hr)(min)(sec)
>> 
>>> newTimeDt <- strptime(Time, format="%d%b%y:%H:%M")
>>> newTimeDt
>> [1] "2011-12-27 00:30:00 PST" "2011-12-27 01:30:00 PST" "2011-12-27 02:00:00 PST"
>>> newTimeDt <- as.POSIXct(Time, format="%d%b%y:%H:%M")
>>> newTimeDt
>> [1] "2011-12-27 00:30:00 PST" "2011-12-27 01:30:00 PST" "2011-12-27 02:00:00 PST"
>>> newOut <- format(newTimeDt, "%Y%m%d%H%M")
>>> newOut
>> [1] "201112270030" "201112270130" "201112270200"
>>> newOut <- format(newTimeDt, "%Y%m%d%H%M%S")
>>> newOut
>> [1] "20111227003000" "20111227013000" "20111227020000"
> 
> Addition to David, if you use functions similar to read.csv or read.table to read dates from a file, you have to set 
> 

I am not opposed to using stringsAsFactors=FALSE at the time of data input, but since there is an as.POSIXct.factor function it was not needed here.


> stringsAsFactors = T

Wrong: It would need to be FALSE to be anything other than the default. And you would not be reading Dates but rather character values.

> 
> in function parameters or set data.frame column as character seperately.

I did test my code on a factor vector.


-- 

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list