tom soyer
2008-Jan-21 01:09 UTC
[R] how to extract data by specific months from a monthly ts object
Hi, I am trying to extract data from a ts object by month, e.g., extract Jan, Feb, and Aug data from a monthly ts object. I tried the following but it didn't work:> xa=1:50 > ta=ts(xa,start=c(1990,1),frequency=12) > ta[cycle(ta)==c(1,2)] # this method works but it's not what I want[1] 1 2 13 14 25 26 37 38 49 50> ta[cycle(ta)==c(1,2,8)] # this method doesn't work, unfortunately[1] 1 2 13 14 25 26 37 38 49 50 Warning message: In `==.default`(cycle(ta), c(1, 2, 8)) : longer object length is not a multiple of shorter object length somehow ta[cycle(ta)==c(1,2)] works. But what I really need is for ta[cycle(ta)==c(1,2,8)] to work, and it doesn't. Does anyone know how could to do this kinds of data extraction? Thanks! -- Tom [[alternative HTML version deleted]]
Marc Schwartz
2008-Jan-21 01:25 UTC
[R] how to extract data by specific months from a monthly ts object
tom soyer wrote:> Hi, > > I am trying to extract data from a ts object by month, e.g., extract Jan, > Feb, and Aug data from a monthly ts object. I tried the following but it > didn't work: > >> xa=1:50 >> ta=ts(xa,start=c(1990,1),frequency=12) >> ta[cycle(ta)==c(1,2)] # this method works but it's not what I want > [1] 1 2 13 14 25 26 37 38 49 50 >> ta[cycle(ta)==c(1,2,8)] # this method doesn't work, unfortunately > [1] 1 2 13 14 25 26 37 38 49 50 > Warning message: > In `==.default`(cycle(ta), c(1, 2, 8)) : > longer object length is not a multiple of shorter object length > > somehow ta[cycle(ta)==c(1,2)] works. But what I really need is for > ta[cycle(ta)==c(1,2,8)] to work, and it doesn't. Does anyone know how could > to do this kinds of data extraction? > > Thanks!Tom, You need to use %in% when you want to test whether one or more values are in a second set of values. In this case:> ta[cycle(ta) %in% c(1, 2, 8)][1] 1 2 8 13 14 20 25 26 32 37 38 44 49 50 BTW, the use of:> ta[cycle(ta) == c(1, 2)][1] 1 2 13 14 25 26 37 38 49 50 results in a correct response, but largely by chance in this case because of the way in which the data is stored. You should be using:> ta[cycle(ta) %in% c(1, 2)][1] 1 2 13 14 25 26 37 38 49 50 Note however, that if we use:> ta[cycle(ta) == c(1, 3)][1] 1 13 25 37 49 we do not get the correct answer, which should be:> ta[cycle(ta) %in% c(1, 3)][1] 1 3 13 15 25 27 37 39 49 See ?"%in%" for more information. HTH, Marc Schwartz
Gabor Grothendieck
2008-Jan-21 01:27 UTC
[R] how to extract data by specific months from a monthly ts object
Try %in% ta[ cycle(ta) %in% c(1, 2, 8) ] cycle(ta) == c(1, 2) works only in this case since c(1, 2) is recycled to cycle(ta) == rep(c(1, 2), length = length(ta)) It would not have worked had your data had started on an even numbered month. This also works: cyc <- cycle(ta) ta[ cyc == 1 | cyc == 2 | cyc == 8 ] On Jan 20, 2008 8:09 PM, tom soyer <tom.soyer at gmail.com> wrote:> Hi, > > I am trying to extract data from a ts object by month, e.g., extract Jan, > Feb, and Aug data from a monthly ts object. I tried the following but it > didn't work: > > > xa=1:50 > > ta=ts(xa,start=c(1990,1),frequency=12) > > ta[cycle(ta)==c(1,2)] # this method works but it's not what I want > [1] 1 2 13 14 25 26 37 38 49 50 > > ta[cycle(ta)==c(1,2,8)] # this method doesn't work, unfortunately > [1] 1 2 13 14 25 26 37 38 49 50 > Warning message: > In `==.default`(cycle(ta), c(1, 2, 8)) : > longer object length is not a multiple of shorter object length > > somehow ta[cycle(ta)==c(1,2)] works. But what I really need is for > ta[cycle(ta)==c(1,2,8)] to work, and it doesn't. Does anyone know how could > to do this kinds of data extraction? > > Thanks! > > -- > Tom > > [[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. >