[R] how to choose dates data?

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Jul 4 13:08:42 CEST 2013


On 04/07/2013 11:36, Dante.py wrote:
> They are really good methods. Thank you very much.
> Could I ask one more question. Is there obvious difference between these 
> two methods?

Yes.  It is obvious that the type of x differs.

> 
> 
> 2013/7/4 Prof Brian Ripley <ripley at stats.ox.ac.uk 
> <mailto:ripley at stats.ox.ac.uk>>
> 
>     On 04/07/2013 08:04, Dante.py wrote:
> 
>         I have a method which is not so smart --use grep to match the
>         pattern.
>         for example:
>         dates <- c("02/27/92", "02/27/92", "01/14/92", "02/28/92",
>         "02/01/91")
>         day <- as.Date(dates, "%m/%d/%y")
>         day:
>         [1] "1992-02-27" "1992-02-27" "1992-01-14" "1992-02-28" "1991-02-01"
>         If I want to search for 1991, I can use:
>         grep("1991-*", day)
> 
> 
>         Hope for a better solution.
> 
> 
>     Here are 2.  The first is quite obvious, the second is faster but
>     needs more knowledge.
> 
>     x <- format(day, "%Y")
>     day[x >= "2007" & x <= "2009"]
> 
>     x <- as.POSIXlt(day)$year + 1900
>     day[x >= 2007 & x <= 2009]
> 
> 
> 
> 
>         2013/7/4 Gallon Li <gallon.li at gmail.com
>         <mailto:gallon.li at gmail.com>>
> 
>             i have converted my data into date format like below:
> 
>                 day=as.Date(originaldate,"%m/%__d/%Y")
>                 day[1:5]
> 
>             [1] "2008-04-12" "2011-07-02" "2011-09-02" "2008-04-12"
>             "2008-04-12"
> 
>             I wish to select only those observations from 2007 to 2009,
>             how can I
>             select from this list?
> 
>                       [[alternative HTML version deleted]]
> 
>             ________________________________________________
>             R-help at r-project.org <mailto:R-help at r-project.org> mailing list
>             https://stat.ethz.ch/mailman/__listinfo/r-help
>             <https://stat.ethz.ch/mailman/listinfo/r-help>
>             PLEASE do read the posting guide
>             http://www.R-project.org/__posting-guide.html
>             <http://www.R-project.org/posting-guide.html>
>             and provide commented, minimal, self-contained, reproducible
>             code.
> 
> 
> 
> 
> 
> 
>         ________________________________________________
>         R-help at r-project.org <mailto:R-help at r-project.org> mailing list
>         https://stat.ethz.ch/mailman/__listinfo/r-help
>         <https://stat.ethz.ch/mailman/listinfo/r-help>
>         PLEASE do read the posting guide
>         http://www.R-project.org/__posting-guide.html
>         <http://www.R-project.org/posting-guide.html>
>         and provide commented, minimal, self-contained, reproducible code.
> 
> 
> 
>     -- 
>     Brian D. Ripley, ripley at stats.ox.ac.uk <mailto:ripley at stats.ox.ac.uk>
>     Professor of Applied Statistics,
>     http://www.stats.ox.ac.uk/~__ripley/
>     <http://www.stats.ox.ac.uk/~ripley/>
>     University of Oxford,             Tel: +44 1865 272861
>     <tel:%2B44%201865%20272861> (self)
>     1 South Parks Road, +44 1865 272866 <tel:%2B44%201865%20272866> (PA)
>     Oxford OX1 3TG, UK                Fax: +44 1865 272595
>     <tel:%2B44%201865%20272595>
> 
> 
> 
> 
> -- 
> 张晔 Dante.py
> 中山大学09级数学与应用数学专业
> 个人主页:http://dantepy.yslsg.org/


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list