[R] POSIX primer

Doran, Harold HDoran at air.org
Fri Apr 2 17:02:02 CEST 2010


Beautiful. Thank you.

-----Original Message-----
From: Gabor Grothendieck [mailto:ggrothendieck at gmail.com] 
Sent: Friday, April 02, 2010 10:59 AM
To: Doran, Harold
Cc: r-help at r-project.org
Subject: Re: [R] POSIX primer

On Fri, Apr 2, 2010 at 10:49 AM, Doran, Harold <HDoran at air.org> wrote:
> I have not used POSIX classes previously and now have a need to use them. I have sports data with times of some athletes

The main reason to use POSIXct is if you need time zones.
If you don't then you might be better off with chron.  See R News 4/1.

> library(chron)
> tt <- times(c('00:14:15', '00:16:45'))
> summary(tt)
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max.
00:14:15 00:14:52 00:15:30 00:15:30 00:16:08 00:16:45
> min(tt); max(tt); mean(tt)
[1] 00:14:15
[1] 00:16:45
[1] 00:15:30

> rank(tt)
[1] 1 2
> order(tt)
[1] 1 2
> sort(tt)
[1] 00:14:15 00:16:45
> tt[order(tt)]
[1] 00:14:15 00:16:45

> after different events. I need to perform some simple analyses using the times. I think I've figured out how to do this. I just want to confirm with others who have more experience that this is indeed the correct approach. If not, please suggest a more appropriate way.
>
> Suppose I have times for two athletes after event 1.
>
>> times <- c('14:15', '16:45')
>
> Now, I use strptime() as follows
>
>>  x <- strptime(times, "%M:%S")
>> x
> [1] "2010-04-02 00:14:15" "2010-04-02 00:16:45"
>
>> class(x)
> [1] "POSIXt"  "POSIXlt"
>
> Now, I want the average time across all athletes as well as the min and max, so I do:
>
>> mean(x); min(x); max(x)
> [1] "2010-04-02 00:15:30 EDT"
> [1] "2010-04-02 00:14:15 EDT"
> [1] "2010-04-02 00:16:45 EDT"
>
> Now, I want to rank order the athletes:
>
>> rank(x)
> Error in if (xi == xj) 0L else if (xi > xj) 1L else -1L :
>  missing value where TRUE/FALSE needed
>
> But, I can rank order the following.
>
>> rank(times)
> [1] 1 2
>
> I don't need the date in the object x, but I can't figure out how to remove it. Nonetheless, it doesn't seem to affect anything.
>
>> x
> [1] "2010-04-02 00:14:15" "2010-04-02 00:16:45"
>
> Is this the right approach for using time variables and performing some computations on them? Or, is there another approach I should look at.
>
> Thanks,
> Harold
>
>> sessionInfo()
> R version 2.10.0 (2009-10-26)
> i386-pc-mingw32
>
> locale:
> [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252
> [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
> [5] LC_TIME=English_United States.1252
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
> other attached packages:
> [1] lme4_0.999375-32   Matrix_0.999375-31 lattice_0.17-26
>
> loaded via a namespace (and not attached):
> [1] grid_2.10.0  tools_2.10.0
>
>        [[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.
>



More information about the R-help mailing list