The vector, vec, below represents a time series of the number of some events recorded for 208 two-week intervals from 2005/01/01 up to the end of 2012. I want to represent this together with the date information. I tried ts(), but don't quite understand how to use the start=, end= and frequency= arguments in this case. vec <- c( 1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 2L, 0L, 1L, 0L, 3L, 1L, 1L, 2L, 0L, 0L, 1L, 0L, 0L, 0L, 3L, 0L, 2L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 3L, 0L, 0L, 3L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 3L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 1L, 0L, 0L, 0L, 2L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 2L, 0L, 2L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 2L, 0L, 1L, 1L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 2L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 2L, 0L, 1L, 1L, 1L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 2L, 0L, 0L, 1L, 0L, 0L, 0L, 2L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L) This is what I tried, but it doesn't seem right because str() reports ... to 2014. > vec.ts <- ts(vec, + start=c(2005,1), + frequency=24 + ) > > str(vec.ts) Time-Series [1:208] from 2005 to 2014: 1 0 0 0 1 1 1 0 2 0 ... > If instead of a time series, I attached date names to the observations, they would be something like names(vect) <- seq(as.Date('2005/01/01'),as.Date('2013/01/01'),by=14) but that's awkward to work with. -- Michael Friendly Email: friendly AT yorku DOT ca Professor, Psychology Dept. & Chair, Quantitative Methods York University Voice: 416 736-2100 x66249 Fax: 416 736-5814 4700 Keele Street Web: http://www.datavis.ca Toronto, ONT M3J 1P3 CANADA
If the data are two week intervals, the frequency is 52/2 = 26 and that matches the amount of data 8*26=208:> vec.ts <- ts(vec, start=c(2005, 1), frequency=26) > str(vec.ts)Time-Series [1:208] from 2005 to 2013: 1 0 0 0 1 1 1 0 2 0 ...> print(vec.ts, calendar=TRUE)p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 p12 p13 p14 p15 p16 2005 1 0 0 0 1 1 1 0 2 0 1 0 3 1 1 2 2006 1 0 1 0 0 1 1 0 1 0 1 1 0 0 3 0 2007 1 0 0 0 3 0 1 1 1 0 0 0 1 0 0 0 2008 0 1 0 1 0 1 0 1 1 0 0 0 1 0 0 1 2009 0 1 1 0 0 0 0 2 0 1 1 0 0 2 0 0 2010 1 0 1 1 0 2 0 1 1 0 1 0 0 0 1 1 2011 1 0 1 0 0 1 1 1 2 0 1 1 1 0 0 2 2012 1 0 0 0 0 1 2 0 0 1 0 0 0 2 1 1 p17 p18 p19 p20 p21 p22 p23 p24 p25 p26 2005 0 0 1 0 0 0 3 0 2 0 2006 0 3 1 1 1 0 1 1 0 1 2007 0 2 0 0 1 0 0 0 2 1 2008 0 0 1 1 0 1 0 2 0 2 2009 0 0 1 0 0 1 1 0 1 0 2010 0 0 0 0 0 0 0 0 0 0 2011 0 0 0 0 1 1 1 0 1 0 2012 0 0 0 0 0 1 1 1 1 0 But str() seems to round up on the year for some reason. ------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Michael Friendly Sent: Thursday, February 20, 2014 2:04 PM To: R-help Subject: [R] bi-monthly time series The vector, vec, below represents a time series of the number of some events recorded for 208 two-week intervals from 2005/01/01 up to the end of 2012. I want to represent this together with the date information. I tried ts(), but don't quite understand how to use the start=, end= and frequency= arguments in this case. vec <- c( 1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 2L, 0L, 1L, 0L, 3L, 1L, 1L, 2L, 0L, 0L, 1L, 0L, 0L, 0L, 3L, 0L, 2L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 3L, 0L, 0L, 3L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 3L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 1L, 0L, 0L, 0L, 2L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 2L, 0L, 2L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 2L, 0L, 1L, 1L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 2L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 2L, 0L, 1L, 1L, 1L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 2L, 0L, 0L, 1L, 0L, 0L, 0L, 2L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L) This is what I tried, but it doesn't seem right because str() reports ... to 2014. > vec.ts <- ts(vec, + start=c(2005,1), + frequency=24 + ) > > str(vec.ts) Time-Series [1:208] from 2005 to 2014: 1 0 0 0 1 1 1 0 2 0 ... > If instead of a time series, I attached date names to the observations, they would be something like names(vect) <- seq(as.Date('2005/01/01'),as.Date('2013/01/01'),by=14) but that's awkward to work with. -- Michael Friendly Email: friendly AT yorku DOT ca Professor, Psychology Dept. & Chair, Quantitative Methods York University Voice: 416 736-2100 x66249 Fax: 416 736-5814 4700 Keele Street Web: http://www.datavis.ca Toronto, ONT M3J 1P3 CANADA ______________________________________________ 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.
Hi Michael What about using zoo you can convert to ts if required library(zoo) tv <- seq(as.Date('2005/01/01'),as.Date('2013/01/01'),by=14) zt <- zoo(vec,tv) head(zt) 2005-01-01 2005-01-15 2005-01-29 2005-02-12 2005-02-26 2005-03-12 1 0 0 0 1 1 Regards Duncan Duncan Mackay Department of Agronomy and Soil Science University of New England Armidale NSW 2351 Email: home: mackay at northnet.com.au -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Michael Friendly Sent: Friday, 21 February 2014 06:04 To: R-help Subject: [R] bi-monthly time series The vector, vec, below represents a time series of the number of some events recorded for 208 two-week intervals from 2005/01/01 up to the end of 2012. I want to represent this together with the date information. I tried ts(), but don't quite understand how to use the start=, end= and frequency= arguments in this case. vec <- c( 1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 2L, 0L, 1L, 0L, 3L, 1L, 1L, 2L, 0L, 0L, 1L, 0L, 0L, 0L, 3L, 0L, 2L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 3L, 0L, 0L, 3L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 3L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 1L, 0L, 0L, 0L, 2L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 2L, 0L, 2L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 2L, 0L, 1L, 1L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 2L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 2L, 0L, 1L, 1L, 1L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 2L, 0L, 0L, 1L, 0L, 0L, 0L, 2L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L) This is what I tried, but it doesn't seem right because str() reports ... to 2014. > vec.ts <- ts(vec, + start=c(2005,1), + frequency=24 + ) > > str(vec.ts) Time-Series [1:208] from 2005 to 2014: 1 0 0 0 1 1 1 0 2 0 ... > If instead of a time series, I attached date names to the observations, they would be something like names(vect) <- seq(as.Date('2005/01/01'),as.Date('2013/01/01'),by=14) but that's awkward to work with. -- Michael Friendly Email: friendly AT yorku DOT ca Professor, Psychology Dept. & Chair, Quantitative Methods York University Voice: 416 736-2100 x66249 Fax: 416 736-5814 4700 Keele Street Web: http://www.datavis.ca Toronto, ONT M3J 1P3 CANADA ______________________________________________ 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.
On Feb 20, 2014, at 12:03 PM, Michael Friendly wrote:> > The vector, vec, below represents a time series of the number of some events recorded > for 208 two-week intervals from 2005/01/01 up to the end of 2012. I want to > represent this together with the date information. I tried ts(), but don't quite understand > how to use the start=, end= and frequency= arguments in this case. > > vec <- c( > 1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 2L, 0L, 1L, 0L, 3L, 1L, 1L, > 2L, 0L, 0L, 1L, 0L, 0L, 0L, 3L, 0L, 2L, 0L, 1L, 0L, 1L, 0L, 0L, > 1L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 3L, 0L, 0L, 3L, 1L, 1L, 1L, > 0L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 3L, 0L, 1L, 1L, 1L, 0L, 0L, > 0L, 1L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 1L, 0L, 0L, 0L, 2L, 1L, 0L, > 1L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, > 0L, 1L, 1L, 0L, 1L, 0L, 2L, 0L, 2L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, > 2L, 0L, 1L, 1L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, > 0L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 2L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, > 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, > 0L, 0L, 1L, 1L, 1L, 2L, 0L, 1L, 1L, 1L, 0L, 0L, 2L, 0L, 0L, 0L, > 0L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 2L, 0L, 0L, > 1L, 0L, 0L, 0L, 2L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, > 0L) > > This is what I tried, but it doesn't seem right because str() reports ... to 2014. > > > vec.ts <- ts(vec, > + start=c(2005,1), > + frequency=24 > + )The nominal number of weeks in a year is 52 so bi-weekly events would be 26 per year:> 365/7/2[1] 26.07143 There is als a seq.Date method taht accepts a vy argument so:> wvec <- data.frame(dt = seq(as.Date("2005-01-01"), by="2 week", length=208), vec) > str(wvec)'data.frame': 208 obs. of 2 variables: $ dt : Date, format: "2005-01-01" "2005-01-15" ... $ vec: int 1 0 0 0 1 1 1 0 2 0 ... Or as mentioned zoo methods which are generally much easier to work with than ts-objects. David.> > > > str(vec.ts) > Time-Series [1:208] from 2005 to 2014: 1 0 0 0 1 1 1 0 2 0 ... > > > > If instead of a time series, I attached date names to the observations, they would be something > like > > names(vect) <- seq(as.Date('2005/01/01'),as.Date('2013/01/01'),by=14) > > but that's awkward to work with. > > -- > Michael Friendly Email: friendly AT yorku DOT ca > Professor, Psychology Dept. & Chair, Quantitative Methods > York University Voice: 416 736-2100 x66249 Fax: 416 736-5814 > 4700 Keele Street Web: http://www.datavis.ca > Toronto, ONT M3J 1P3 CANADA >David Winsemius Alameda, CA, USA