[R] Datetime conversion

Girish A.R. garamach at gmail.com
Fri Sep 18 12:07:49 CEST 2009


See if this is what you are looking for:

dt <- as.data.frame(datetime)
> date<-strptime(as.character(dt$datetime),"%d%b%Y")
> date
[1] "1987-10-01" "2004-04-12" "1987-12-01" "1975-10-01" "1979-08-01"
"2003-06-26"
[7] "1900-01-01" "1998-05-13" "1998-09-30"

cheers,
-Girish

========================================================

premmad wrote:
> 
> Girish it works for me also if its a vector.I have problem if the data is
> stored as dataframe(rows and columns) please do help me in this
> 
> Girish A.R. wrote:
>> 
>> Seems to work alright for me.
>> 
>> datetime <-c(
>> "01OCT1987:00:00:00.000",
>> "12APR2004:00:00:00.000",
>> "01DEC1987:00:00:00.000",
>> "01OCT1975:00:00:00.000",
>> "01AUG1979:00:00:00.000",
>> "26JUN2003:00:00:00.000",
>> "01JAN1900:00:00:00.000",
>> "13MAY1998:00:00:00.000",
>> "30SEP1998:00:00:00.000") 
>> 
>>> date<-strptime(datetime,"%d%b%Y") 
>>> date
>> [1] "1987-10-01" "2004-04-12" "1987-12-01" "1975-10-01" "1979-08-01"
>> "2003-06-26"
>> [7] "1900-01-01" "1998-05-13" "1998-09-30"
>> 
>> cheers,
>> -Girish
>> 
>> 
>> premmad wrote:
>>> 
>>> Thanks for your reply
>>> 
>>> datetime
>>> 01OCT1987:00:00:00.000
>>> 12APR2004:00:00:00.000
>>> 01DEC1987:00:00:00.000
>>> 01OCT1975:00:00:00.000
>>> 01AUG1979:00:00:00.000
>>> 26JUN2003:00:00:00.000
>>> 01JAN1900:00:00:00.000
>>> 13MAY1998:00:00:00.000
>>> 30SEP1998:00:00:00.000
>>> 
>>> is in the test  file and i have imported it in to R and created
>>> dataframe test with variable datetime for extracting only the datepart
>>> from it.
>>> By using the following function 
>>> 
>>> test$date<-strptime(test$datetime,"%d%b%Y")
>>> and i got the following error 
>>> 
>>> Error in `$<-.data.frame`(`*tmp*`, "rw", value = list(sec = c(0, 0, 0, 
>>> : 
>>>   replacement has 9 rows, data has 14
>>> 
>>> Is there any other way to solve this, please do reply .
>>> 
>>> 
>>> David Winsemius wrote:
>>>> 
>>>> 
>>>> On Sep 18, 2009, at 1:03 AM, premmad wrote:
>>>> 
>>>>>
>>>>> I'm relatively new to R .I tried converting the datetime column with  
>>>>> values
>>>>> like 01apr1985:00:00:00.000 using strptime(datetime,"%d%b%Y).Could  
>>>>> anyone
>>>>> help me in this regard .Please reply ASAP i need .
>>>> 
>>>> You will need to give us a more complete sample of your code, because  
>>>> I don't see a problem:
>>>> 
>>>>  > strptime("01apr1985:00:00:00.000","%d%b%Y")
>>>> [1] "1985-04-01"
>>>> 
>>>> also works when hours, minutes and seconds format was used:
>>>> 
>>>>> > strptime("01apr1985:00:00:00.000","%d%b%Y:%H:%M:%S")
>>>>> [1] "1985-04-01"
>>>>> > strptime("01apr1985:00:00:00.000","%d%b%Y:%T")
>>>>> [1] "1985-04-01"
>>>>>
>>>> 
>>>> David Winsemius, MD
>>>> Heritage Laboratories
>>>> West Hartford, CT
>>>> 
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide
>>>> http://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>> 
>>>> 
>>> 
>>> 
>> 
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Datetime-conversion-tp25503138p25506234.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list