[R] date objects

Ista Zahn istazahn at gmail.com
Tue Feb 18 02:35:08 CET 2014


Hi Erin,

"Best" is subjective, but I like

library(lubridate)
library(plyr)
xb.df <- mutate(xb.df,
                time = ymd(time, truncated = 1),
                year = year(time),
                quarter = quarter(time))

Best,
Ista


On Mon, Feb 17, 2014 at 7:35 PM, Erin Hodgess <erinm.hodgess at gmail.com> wrote:
> Hello!
>
> I  generate the following two data frames:
>
>> xa.df <- data.frame(x=rnorm(10),time=2001:2010)
>> xa.df
>              x time
> 1  -1.73207086 2001
> 2  -0.84014049 2002
> 3  -0.02332163 2003
> 4  -1.47236670 2004
> 5  -0.17736265 2005
> 6  -1.27134314 2006
> 7  -2.53425374 2007
> 8   0.79132952 2008
> 9   1.16574188 2009
> 10  1.99660600 2010
>> tim1 <- c("2010-01","2010-02","2010-03","2010-04","2011-01","2011-02",
> + "2011-03","2011-04")
>> xb.df <- data.frame(y=rnorm(8),time=tim1)
>> xb.df
>            y    time
> 1 -1.5175587 2010-01
> 2  1.2035292 2010-02
> 3 -0.7313063 2010-03
> 4 -1.5544370 2010-04
> 5 -0.5907269 2011-01
> 6  1.0634754 2011-02
> 7  0.9029293 2011-03
> 8  0.5996780 2011-04
>>
>
> What is the best way to turn the time columns into date objects (annual and
> quarterly)?
>
> Thanks,
> Erin
>
>
> --
> Erin Hodgess
> Associate Professor
> Department of Computer and Mathematical Sciences
> University of Houston - Downtown
> mailto: erinm.hodgess at gmail.com
>
>         [[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