[R] Timestamps and manipulations
jim holtman
jholtman at gmail.com
Mon Oct 13 13:28:18 CEST 2008
Is this what you want:
> x.df
timestamp id
1 2008-05-27 22:57:00 763830873067
2 2008-05-27 23:00:00 763830873067
3 2008-05-27 23:01:00 763830873067
4 2008-05-27 23:01:00 763830873067
5 2008-06-05 11:34:00 763830873067
6 2008-05-29 23:08:00 765253440317
7 2008-05-29 23:06:00 765253440317
8 2008-05-29 22:52:00 765253440317
9 2008-05-29 22:52:00 765253440317
10 2008-05-29 23:04:00 765253440317
11 2008-06-27 19:34:00 765253440317
12 2008-07-09 15:45:00 765329002557
13 2008-07-06 19:24:00 765329002557
14 2008-07-09 15:46:00 765329002557
15 2008-07-07 13:05:00 765329002557
16 2008-05-16 22:40:00 765329002557
17 2008-06-08 11:24:00 765329002557
18 2008-06-08 12:33:00 765329002557
> x.df$time <- ifelse(x.df$timestamp > as.POSIXct("2008-07-01"), 1, 0)
> x.df
timestamp id time
1 2008-05-27 22:57:00 763830873067 0
2 2008-05-27 23:00:00 763830873067 0
3 2008-05-27 23:01:00 763830873067 0
4 2008-05-27 23:01:00 763830873067 0
5 2008-06-05 11:34:00 763830873067 0
6 2008-05-29 23:08:00 765253440317 0
7 2008-05-29 23:06:00 765253440317 0
8 2008-05-29 22:52:00 765253440317 0
9 2008-05-29 22:52:00 765253440317 0
10 2008-05-29 23:04:00 765253440317 0
11 2008-06-27 19:34:00 765253440317 0
12 2008-07-09 15:45:00 765329002557 1
13 2008-07-06 19:24:00 765329002557 1
14 2008-07-09 15:46:00 765329002557 1
15 2008-07-07 13:05:00 765329002557 1
16 2008-05-16 22:40:00 765329002557 0
17 2008-06-08 11:24:00 765329002557 0
18 2008-06-08 12:33:00 765329002557 0
> # time difference by id
> sapply(split(x.df$timestamp, x.df$id), function(.time){
+ difftime(max(.time), min(.time), units='secs')
+ })
763830873067 765253440317 765329002557
736620 2493720 4640760
>
>
On Mon, Oct 13, 2008 at 6:57 AM, Michael Pearmain <mpearmain at google.com> wrote:
> Hi All,
> I've a couple of questions i've been struggling with using the time
> features, can anyone help? sample data
>
> Timestamp user_id
> 27/05/08 22:57 763830873067 27/05/08 23:00 763830873067 27/05/08 23:01
> 763830873067 27/05/08 23:01 763830873067 05/06/08 11:34 763830873067
> 29/05/08
> 23:08 765253440317 29/05/08 23:06 765253440317 29/05/08 22:52
> 765253440317 29/05/08
> 22:52 765253440317 29/05/08 23:04 765253440317 27/06/08 19:34
> 765253440317 09/07/08
> 15:45 765329002557 06/07/08 19:24 765329002557 09/07/08 15:46
> 765329002557 07/07/08
> 13:05 765329002557 16/05/08 22:40 765329002557 08/06/08 11:24
> 765329002557 08/06/08
> 12:33 765329002557
>
> My first question is how can i create a new var creating a filter based on a
> date?
>
> I've tried as.POSIXct.strptime below as well but to no avail.. can anyone
> give any advice?
>
>>Mcookie$timestamp <- as.POSIXct(strptime(Mcookie$timestamp,"%m/%d/%Y
> %H:%M"))
>>Mcookie$time <- ifelse(Mcookie$timestamp >
> strptime("07-08-2008-00:00","%m-%d-%Y-%H:%M",1,0)
>
> My second questions refers to finding the time difference in seconds between
> the first time a user sees something Vs the last.. and engagment time
> essentially,
> i see there is the difftime function, is there a more elegant way of
> working this out then my thoughts (Pysdo code below)
>
> sort data by user_id and Timestamp
> take the head of user_id as new_time_var
> take the tail of user_id as new_time_var2
> use difftime(new_time_var, new_time_var2, units="secs")
>
> Mike
>
> [[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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
More information about the R-help
mailing list