use POSIXct instead of Date:
> x <- c("2013-12-12 12:00:00", "2013-12-15 03:15:23")
> # convert using as.POSIXct
>
> times <- as.POSIXct(x, format = "%Y-%m-%d %H:%M:%S")
> times
[1] "2013-12-12 12:00:00 EST" "2013-12-15 03:15:23
EST">
> difftime(times[2], times[1], units = 'secs')
Time difference of 227723 secs>
>
Jim Holtman
Data Munger Guru
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
On Wed, Dec 18, 2013 at 9:24 PM, Lydia Keppler <lydiakeppler at gmail.com>
wrote:> Hi there,
>
> I am new to R and programming in general and am looking for help with
> writing a function with dates and times. I have checked around but am still
> a bit stuck.
>
> Basically, I have data in the format "dd/mm/YYYY HH:MM" and I
have to
> calculate how much time has passed between various events.
>
> I have given the following command ( where "date" is the column
in my data
> frame that indicates the date and time)
> date=as.Date.factor(date,format="%d/%m/%Y %H:%M")
> However, this displays only the date, without the time.
>
> I also have tried:
> date=substr(argo1$date,1,907)
> And it shows the date and time.
>
> However, when I try to find the difference between two dates i.e.the time
> that has passed with the command:
difftime(date[2],date[3],unit="secs"), it
> returns that 0 seconds have passed.
>
> When I try to find the difference with the command:
> date[3]-date[2]
> it tells me Error in date[3] - date[2] : non-numeric argument to binary
> operator
>
> The class(date) is "character".
>
> Any idea what I am doing wrong?
> Thank you very much in advance!
>
>
> On 19 December 2013 15:22, <r-help-request at r-project.org> wrote:
>
>> Welcome to the R-help at r-project.org mailing list!
>>
>> To post to this list, send your message to:
>>
>> r-help at r-project.org
>>
>> General information about the mailing list is at:
>>
>> https://stat.ethz.ch/mailman/listinfo/r-help
>>
>> If you ever want to unsubscribe or change your options (eg, switch to
>> or from digest mode, change your password, etc.), visit your
>> subscription page at:
>>
>> https://stat.ethz.ch/mailman/options/r-help/lydiakeppler%40gmail.com
>>
>> You can also make such adjustments via email by sending a message to:
>>
>> R-help-request at r-project.org
>>
>> with the word `help' in the subject or body (don't include the
>> quotes), and you will get back a message with instructions.
>>
>> You must know your password to change your options (including changing
>> the password, itself) or to unsubscribe without confirmation. It is:
>>
>> whaletail27
>>
>> Normally, Mailman will remind you of your r-project.org mailing list
>> passwords once every month, although you can disable this if you
>> prefer. This reminder will also include instructions on how to
>> unsubscribe or change your account options. There is also a button on
>> your options page that will email your current password to you.
>>
>
>
>
> --
> *Protect the environment: think before you print*
>><((((?>
>
> [[alternative HTML version deleted]]
>
>
> ______________________________________________
> 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.
>