Re: [R] Calculating date difference in days

2019-05-22 Thread Jeff Newmiller
At the point where you say "date difference in days" IMO you have departed from 
what `difftime` is for and are in the realm of a numeric measure. I ignore the 
units inside `difftime` at all times and convert to numeric with a units 
argument if I want to be that specific about how the measure is represented.

You may or may not recall the difference between angle ABC and the measure of 
angle ABC (with a bar over it) from geometry... but the idea is the same... 
distinguish the thing (time interval) from the numbers used to quantify it 
(numeric).

elapsed_days <- function(end_date, start_date){
  ed <- as.POSIXlt(end_date) 
  sd <- as.POSIXlt(start_date) 
  as.numeric( ed-sd, units="days" )
}


On May 22, 2019 2:43:42 PM PDT, reichm...@sbcglobal.net wrote:
>R Help
>
>I have a function to calculate a date difference in days but my results
>come
>back in hours.  I suspect I am using the as.POSIXlt function
>incorrectly . 
>
>Suggestions?
>
># Start time of data to be considered 
>start_day <- "2016-04-30"
>
># Make event and sequence IDs into factors 
>elapsed_days <- function(end_date, start_date){
>  ed <- as.POSIXlt(end_date) 
>  sd <- as.POSIXlt(start_date) 
>  ed-sd 
>}
>
>trans_sequence$eventID <- elapsed_days(trans_sequence$Date, start_day)
>
>
>> trans_sequence
># A tibble: 39 x 5
># Groups:   Emitter [15]
>   Emitter DateSIZE Geohash
>eventID  
>
>   
> 1   1 2016-05-0112 A;B;C;D;E;F;G;H;I;J;K;L
>19 hours
> 2   1 2016-05-02 5 A;B;C;D;E
>43 hours
> 3   1 2016-05-0511 A;B;C;D;E;F;G;H;I;J;K
>115 hours
> 4   2 2016-05-01 9 C;D;E;F;G;H;I;J;K
>19 hours
> 5   2 2016-05-02 3 F;G;H
>43 hours
> 6   2 2016-05-05 3 L;M;N
>115 hours
> 7   3 2016-05-01 3 L;M;N
>19 hours
> 8   3 2016-05-02 3 I;J;K
>43 hours
> 9   3 2016-05-0425
>A;B;C;D;E;F;G;H;I;J;K;L;M;N;O;P;Q;R;S;T;U;V;W;X;Y  91 hours
>10   3 2016-05-05 7 O;P;Q;R;S;T;U
>115 hours
>
>Jeff Reichman
>
>
>   [[alternative HTML version deleted]]
>
>__
>R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>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.

-- 
Sent from my phone. Please excuse my brevity.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Calculating date difference in days

2019-05-22 Thread William Dunlap via R-help
You can use units<- to change the time units of the difference.  E.g.,

> d <- as.POSIXlt("2018-03-10") -  as.POSIXlt("2018-03-09 02:00:00")
> d
Time difference of 22 hours
> units(d) <- "days"
> d
Time difference of 0.917 days
>
> units(d) <- "mins"
> d
Time difference of 1320 mins
> units(d) <- "secs"
> d
Time difference of 79200 secs
> units(d) <- "weeks"
> d
Time difference of 0.1309524 weeks

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Wed, May 22, 2019 at 2:44 PM  wrote:

> R Help
>
> I have a function to calculate a date difference in days but my results
> come
> back in hours.  I suspect I am using the as.POSIXlt function incorrectly .
>
> Suggestions?
>
> # Start time of data to be considered
> start_day <- "2016-04-30"
>
> # Make event and sequence IDs into factors
> elapsed_days <- function(end_date, start_date){
>   ed <- as.POSIXlt(end_date)
>   sd <- as.POSIXlt(start_date)
>   ed-sd
> }
>
> trans_sequence$eventID <- elapsed_days(trans_sequence$Date, start_day)
>
>
> > trans_sequence
> # A tibble: 39 x 5
> # Groups:   Emitter [15]
>Emitter DateSIZE Geohash
> eventID
> 
> 
>  1   1 2016-05-0112 A;B;C;D;E;F;G;H;I;J;K;L
> 19 hours
>  2   1 2016-05-02 5 A;B;C;D;E
> 43 hours
>  3   1 2016-05-0511 A;B;C;D;E;F;G;H;I;J;K
> 115 hours
>  4   2 2016-05-01 9 C;D;E;F;G;H;I;J;K
> 19 hours
>  5   2 2016-05-02 3 F;G;H
> 43 hours
>  6   2 2016-05-05 3 L;M;N
> 115 hours
>  7   3 2016-05-01 3 L;M;N
> 19 hours
>  8   3 2016-05-02 3 I;J;K
> 43 hours
>  9   3 2016-05-0425
> A;B;C;D;E;F;G;H;I;J;K;L;M;N;O;P;Q;R;S;T;U;V;W;X;Y  91 hours
> 10   3 2016-05-05 7 O;P;Q;R;S;T;U
> 115 hours
>
> Jeff Reichman
>
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Calculating date difference in days

2019-05-22 Thread Boris Steipe
ed <- as.POSIXlt("2018-03-10")
sd <- as.POSIXlt("2018-02-10")
as.numeric(ed-sd)
[1] 28

ed <- as.POSIXlt("2000-03-10")
sd <- as.POSIXlt("2000-02-10")
as.numeric(ed-sd)
[1] 29

Cheers,
B.


> On 2019-05-22, at 17:43, reichm...@sbcglobal.net wrote:
> 
> R Help
> 
> I have a function to calculate a date difference in days but my results come
> back in hours.  I suspect I am using the as.POSIXlt function incorrectly . 
> 
> Suggestions?
> 
> # Start time of data to be considered 
> start_day <- "2016-04-30"
> 
> # Make event and sequence IDs into factors 
> elapsed_days <- function(end_date, start_date){
>  ed <- as.POSIXlt(end_date) 
>  sd <- as.POSIXlt(start_date) 
>  ed-sd 
> }
> 
> trans_sequence$eventID <- elapsed_days(trans_sequence$Date, start_day)
> 
> 
>> trans_sequence
> # A tibble: 39 x 5
> # Groups:   Emitter [15]
>   Emitter DateSIZE Geohash
> eventID  
>
>
> 1   1 2016-05-0112 A;B;C;D;E;F;G;H;I;J;K;L
> 19 hours
> 2   1 2016-05-02 5 A;B;C;D;E
> 43 hours
> 3   1 2016-05-0511 A;B;C;D;E;F;G;H;I;J;K
> 115 hours
> 4   2 2016-05-01 9 C;D;E;F;G;H;I;J;K
> 19 hours
> 5   2 2016-05-02 3 F;G;H
> 43 hours
> 6   2 2016-05-05 3 L;M;N
> 115 hours
> 7   3 2016-05-01 3 L;M;N
> 19 hours
> 8   3 2016-05-02 3 I;J;K
> 43 hours
> 9   3 2016-05-0425
> A;B;C;D;E;F;G;H;I;J;K;L;M;N;O;P;Q;R;S;T;U;V;W;X;Y  91 hours
> 10   3 2016-05-05 7 O;P;Q;R;S;T;U
> 115 hours
> 
> Jeff Reichman
> 
> 
>   [[alternative HTML version deleted]]
> 
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


[R] Calculating date difference in days

2019-05-22 Thread reichmanj
R Help

I have a function to calculate a date difference in days but my results come
back in hours.  I suspect I am using the as.POSIXlt function incorrectly . 

Suggestions?

# Start time of data to be considered 
start_day <- "2016-04-30"

# Make event and sequence IDs into factors 
elapsed_days <- function(end_date, start_date){
  ed <- as.POSIXlt(end_date) 
  sd <- as.POSIXlt(start_date) 
  ed-sd 
}

trans_sequence$eventID <- elapsed_days(trans_sequence$Date, start_day)


> trans_sequence
# A tibble: 39 x 5
# Groups:   Emitter [15]
   Emitter DateSIZE Geohash
eventID  

   
 1   1 2016-05-0112 A;B;C;D;E;F;G;H;I;J;K;L
19 hours
 2   1 2016-05-02 5 A;B;C;D;E
43 hours
 3   1 2016-05-0511 A;B;C;D;E;F;G;H;I;J;K
115 hours
 4   2 2016-05-01 9 C;D;E;F;G;H;I;J;K
19 hours
 5   2 2016-05-02 3 F;G;H
43 hours
 6   2 2016-05-05 3 L;M;N
115 hours
 7   3 2016-05-01 3 L;M;N
19 hours
 8   3 2016-05-02 3 I;J;K
43 hours
 9   3 2016-05-0425
A;B;C;D;E;F;G;H;I;J;K;L;M;N;O;P;Q;R;S;T;U;V;W;X;Y  91 hours
10   3 2016-05-05 7 O;P;Q;R;S;T;U
115 hours

Jeff Reichman


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.