Reputation: 2520
I have a dataset, which has ts()
class.
The dput
looks like this:
structure(c(0.0499999999999998, -0.0499999999999998, 0.12, 0.28,
0.109999999999999, 0.0500000000000007, -0.44, 0.16, -0.11, -0.8,
-0.24, 0.0800000000000001, -0.13, -0.19, 0.0500000000000007,
0.56, -0.2, -0.0299999999999994, -0.130000000000001, -0.109999999999999,
-0.0700000000000003, 0.0499999999999998, 0.100000000000001, -0.04,
-0.0200000000000005, 0.13, -0.21, -0.149999999999999, -0.180000000000001,
-0.13, -0.0800000000000001, -0.13, 0.0800000000000001, 0.41,
0.0499999999999998, 0.19, -0.11, 0, -0.2, -0.13, 0.0300000000000002,
-0.27, -0.159999999999999, -0.04, -0.0800000000000001, -0.0300000000000002,
-0.04, -0.0299999999999998, 0.0600000000000001, -0.04, -0.11,
-0.13, -0.12, 0.0500000000000003, -0.1, -0.12, -0.0299999999999998,
-0.0100000000000002, 0.0700000000000003, 0.12, 0.0300000000000002,
-0.11, 0.0899999999999999, 0.53, 0.3, 0.0899999999999999, 0.0300000000000002,
-0.3, 0.0699999999999994, 0.2, -0.0300000000000002, -0.13, 0.04,
0, -0.149999999999999, -0.0300000000000002, -0.0300000000000002,
-0.00999999999999979, 0.04, -0.12, -0.04, -0.14, -0.19, 0.04,
0.0600000000000001, -0.1, 0.17, 0.14, 0.0699999999999998, -0.14,
-0.02, -0.0900000000000003, 0.14, 0.02, -0.0899999999999999,
-0.21, 0.0299999999999998, -0.0899999999999999, 0, -0.0300000000000002,
-0.13, 0, 0.02, 0.0100000000000002, 0.3, 0.43, -0.0499999999999998,
0.0199999999999996, 0.0300000000000002, -0.16, -0.0300000000000002,
-0.11, 0.0700000000000003, -0.0900000000000003, -0.0800000000000001,
0.1, 0.02, 0.0300000000000002, 0.0800000000000001, 0.3, 0.11,
0.0299999999999994, 0.12, -0.0199999999999996, -0.04, 0.0199999999999996,
0.0800000000000001, 0.2, 0.04, -0.23, -0.18, -0.0899999999999999,
-0.11, -0.12, -0.0699999999999994, -0.27, -0.0299999999999998,
-0.15, -0.02, 0.0899999999999999, 0.0100000000000002, 0.02, -0.1,
-0.15, -0.02, -0.14, -0.0800000000000001, -0.0699999999999998,
-0.14, -0.0800000000000001), .Tsp = c(2008.16666666667, 2020.58333333333,
12), class = "ts")
Visually it looks like:
I am trying to remove one value for March 2008. But it I use something
foo[-c(1:1)]
it converts data into numeric
and it will not work for me.
How to remove this variable and keep the ts()
class and date values? Thanks!
Upvotes: 1
Views: 1523
Reputation: 270160
Any of these should give the requested result.
The first uses window.ts
to start at the indicated year and month.
The second also uses window.ts
but specifies it in terms of the second time value rather than hard coding it.
The third replaces the first value with NA and then runs na.omit
to remove it. It should work if the series has no NA values.
The fourth adds the series to a zero series which is lagged forward and should work if the series has no NA values.
The fifth converts to zoo class where subscripting works and then converts back.
The sixth is the same as the second except it uses a different expression for the second time point.
window(foo, start = c(2008, 4))
window(foo, start = time(foo)[2])
na.omit(replace(foo, 1, NA))
foo + lag(0 * foo, -1)
library(zoo)
as.ts(as.zoo(foo)[-1])
window(foo, start = tsp(foo)[1] + deltat(foo))
Upvotes: 3
Reputation: 887811
An option is to reconvert back to ts
foo2 <- ts(foo[-1], start = index(foo)[2], frequency = frequency(foo))
-output
foo2
# Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
#2008 -0.05 0.12 0.28 0.11 0.05 -0.44 0.16 -0.11 -0.80
#2009 -0.24 0.08 -0.13 -0.19 0.05 0.56 -0.20 -0.03 -0.13 -0.11 -0.07 0.05
#2010 0.10 -0.04 -0.02 0.13 -0.21 -0.15 -0.18 -0.13 -0.08 -0.13 0.08 0.41
#2011 0.05 0.19 -0.11 0.00 -0.20 -0.13 0.03 -0.27 -0.16 -0.04 -0.08 -0.03
#2012 -0.04 -0.03 0.06 -0.04 -0.11 -0.13 -0.12 0.05 -0.10 -0.12 -0.03 -0.01
#2013 0.07 0.12 0.03 -0.11 0.09 0.53 0.30 0.09 0.03 -0.30 0.07 0.20
#2014 -0.03 -0.13 0.04 0.00 -0.15 -0.03 -0.03 -0.01 0.04 -0.12 -0.04 -0.14
#2015 -0.19 0.04 0.06 -0.10 0.17 0.14 0.07 -0.14 -0.02 -0.09 0.14 0.02
#2016 -0.09 -0.21 0.03 -0.09 0.00 -0.03 -0.13 0.00 0.02 0.01 0.30 0.43
#2017 -0.05 0.02 0.03 -0.16 -0.03 -0.11 0.07 -0.09 -0.08 0.10 0.02 0.03
#2018 0.08 0.30 0.11 0.03 0.12 -0.02 -0.04 0.02 0.08 0.20 0.04 -0.23
#2019 -0.18 -0.09 -0.11 -0.12 -0.07 -0.27 -0.03 -0.15 -0.02 0.09 0.01 0.02
#2020 -0.10 -0.15 -0.02 -0.14 -0.08 -0.07 -0.14 -0.08
or we assign the value to NA
foo[1] <- NA
Upvotes: 0