Skip to content

Question on creating Date variable

6 messages · Christofer Bogaso, Rui Barradas, arun +2 more

#
On 01 January 2013 03:00:18, David Winsemius wrote:
'How could that represent any disclosure of proprietary information if 
presented with no context? ' I must agree with you. But I just dont 
want to take any risk! (job scenario in my country is not very 
optimistic and I want to give my boss minimal chance/reason to fire!)

And secondly with your approach, I cant do any calculation. Let take 
following example:

y <- format( as.POSIXct(as.character(x), format="%H.%M"),  # That is 
the input format
             format="%I.%M %p")

y[3] - y[2]

This gives me following error:

Error in y[3] - y[2] : non-numeric argument to binary operator

I am having same error with Devid's approach as well:
Error in z[2] - z[1] : non-numeric argument to binary operator.

Thanks and regards,
#
Hello,

The format AM/PM should be for display purposes only, when you use 
format() you get a variable of class "character", not of classes 
"POSIXct" "POSIXt" . Produce a variable y with as.POSIXct (without 
AM/PM) for arithmetics and another formated for display.

Hope this helps,

Rui Barradas
Em 01-01-2013 05:40, Christofer Bogaso escreveu:
#
HI,

Just by taking David's solution:
?y <- as.POSIXct(paste( floor(x), round(60*(x-floor(x))) ), format="%H %M")
?y1<-data.frame(y,AM_PM=format(y,format="%p"))
?y1[3,1]-y1[4,1]
#Time difference of -40 mins
?y1[5,1]-y1[3,1]
#Time difference of -13 mins
?head(y1,2)
#??????????????????? y AM_PM
#1 2013-01-01 11:00:00??? AM
#2 2013-01-01 11:15:00??? AM
A.K.





----- Original Message -----
From: Christofer Bogaso <bogaso.christofer at gmail.com>
To: David Winsemius <dwinsemius at comcast.net>; David L Carlson <dcarlson at tamu.edu>
Cc: r-help at r-project.org
Sent: Tuesday, January 1, 2013 12:40 AM
Subject: Re: [R] Question on creating Date variable
On 01 January 2013 03:00:18, David Winsemius wrote:
'How could that represent any disclosure of proprietary information if 
presented with no context? ' I must agree with you. But I just dont 
want to take any risk! (job scenario in my country is not very 
optimistic and I want to give my boss minimal chance/reason to fire!)

And secondly with your approach, I cant do any calculation. Let take 
following example:

y <- format( as.POSIXct(as.character(x), format="%H.%M"),? # That is 
the input format
? ? ? ? ? ?  format="%I.%M %p")

y[3] - y[2]

This gives me following error:

Error in y[3] - y[2] : non-numeric argument to binary operator

I am having same error with Devid's approach as well:
Error in z[2] - z[1] : non-numeric argument to binary operator.

Thanks and regards,

______________________________________________
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.
#
On Dec 31, 2012, at 9:40 PM, Christofer Bogaso wrote:

            
That was my code. If you will need to apply numeric operators then you  
should be storing the POSIXct value as an intermediate:

dt_time <- as.POSIXct(as.character(x), format="%H.%M")

out_time <- format( dt_time,
            format="%I.%M %p")
dt_time[3] - dt_time[2]
#
[1] "2013-01-01 10:30:00 EST" "2013-01-01 11:00:00 EST" "2013-01-01
11:01:00 EST"
[4] "2013-01-01 11:09:00 EST" "2013-01-01 11:15:00 EST" "2013-01-01
11:59:00 EST"
[7] "2013-01-01 12:00:00 EST" "2013-01-01 13:00:00 EST"

        
On Tue, Jan 1, 2013 at 11:09 AM, arun <smartpink111 at yahoo.com> wrote:

  
    
#
On Jan 1, 2013, at 9:02 AM, jim holtman wrote:

            
Useful procedure to prevent loss of trailing ".00"'s, ... but just to  
clarify, sprintf never returns a numeric class object, but rather  
returns a character representation of one. (Which is an appropriate  
class for `as.POSIXct`.)
Just as those values are printed character representations of what is  
internally a numeric vector even though it will not admit to being  
such until coerced:

 > Sys.Date()
[1] "2013-01-01"
 > is.numeric(Sys.Date())
[1] FALSE
 > as.numeric(Sys.Date())
[1] 15706
 > as.numeric(as.POSIXct(Sys.Date()))
[1] 1356998400
 >