Hi Is there an easy way to drop, for instance, the first 4 observation of a time series object in R? I have tried to google the answer without any luck. To be more clear: Let us say that I have a time seres object x which data from 2000Q1 to 2014Q4 but I only want the data from 2001Q1 to 2014Q4.How do I remove the first four elements? By using x[5:?] R no longer recognize it as a ts.object. Thank you Best regards [[alternative HTML version deleted]]
For class 'ts' the 'window' function in the base package will do it.> x <- ts(101:117, start=2001.75, frequency=4) > xQtr1 Qtr2 Qtr3 Qtr4 2001 101 2002 102 103 104 105 2003 106 107 108 109 2004 110 111 112 113 2005 114 115 116 117> window(x, start=2002.5)Qtr1 Qtr2 Qtr3 Qtr4 2002 104 105 2003 106 107 108 109 2004 110 111 112 113 2005 114 115 116 117> window(x, end=2002.5)Qtr1 Qtr2 Qtr3 Qtr4 2001 101 2002 102 103 104 Bill Dunlap TIBCO Software wdunlap tibco.com On Sat, Jan 31, 2015 at 11:16 AM, Mikael Olai Milh?j <mikaelmilhoj at gmail.com> wrote:> Hi > > Is there an easy way to drop, for instance, the first 4 observation of a > time series object in R? I have tried to google the answer without any > luck. > > To be more clear: > Let us say that I have a time seres object x which data from 2000Q1 to > 2014Q4 but I only want the data from 2001Q1 to 2014Q4.How do I remove the > first four elements? > > By using x[5:?] R no longer recognize it as a ts.object. > > Thank you > > Best regards > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]
On Sat, Jan 31, 2015 at 2:16 PM, Mikael Olai Milh?j <mikaelmilhoj at gmail.com> wrote:> Hi > > Is there an easy way to drop, for instance, the first 4 observation of a > time series object in R? I have tried to google the answer without any > luck. > > To be more clear: > Let us say that I have a time seres object x which data from 2000Q1 to > 2014Q4 but I only want the data from 2001Q1 to 2014Q4.How do I remove the > first four elements? > > By using x[5:?] R no longer recognize it as a ts.object. >1. We could convert it to a zoo series, drop the first 4 and convert back: For example, using the built in presidents ts series: library(zoo) as.ts(tail(as.zoo(presidents), -4)) 1a. This would work too: library(zoo) as.ts(as.zoo(presidents)[-(1:4)]) 2. Using only base R one can use window like this since 4 observations is one cycle (given that the frequency of the presidents dataset is 4. window(presidents, start = start(presidents) + 1) or in terms of 4: window(presidents, start = start(presidents) + 4 * deltat(presidents)) Here deltat is the time between observations so we want to start 4 deltat's later. -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Hi, I have a data frame named data and with the statement data[5:length(data[[1]]),] I can get row 5 to the end of the data.> datarole counts 1 Agent 220 2 Theme 169 3 Patient 67 4 Location 41 5 Destination 32 6 Recipient 31 7 Result 29 8 Instrument 27 9 Source 25 10 Experiencer 22 11 Topic 22 12 Stimulus 18 13 Attribute 15 14 Beneficiary 12 15 Initial_Location 12 16 Co-Agent 11 17 Co-Patient 11 18 Co-Theme 9 19 Goal 9 20 Asset 7 21 Cause 6 22 Material 5 23 Value 5 24 Product 4 25 Predicate 3 26 Trajectory 3 27 Extent 2 28 Time 2 29 Reflexive 1> data[5:length(data[[1]]),]role counts 5 Destination 32 6 Recipient 31 7 Result 29 8 Instrument 27 9 Source 25 10 Experiencer 22 11 Topic 22 12 Stimulus 18 13 Attribute 15 14 Beneficiary 12 15 Initial_Location 12 16 Co-Agent 11 17 Co-Patient 11 18 Co-Theme 9 19 Goal 9 20 Asset 7 21 Cause 6 22 Material 5 23 Value 5 24 Product 4 25 Predicate 3 26 Trajectory 3 27 Extent 2 28 Time 2 29 Reflexive 1 -- View this message in context: http://r.789695.n4.nabble.com/Dropping-time-series-observations-tp4702589p4702680.html Sent from the R help mailing list archive at Nabble.com.