Here is the data:
e <- data.frame( date = c("2016-03-08", "2016-05-19" ,"2016-05-19" ,"2016-09-02" ,"2016-09-02", "2016-11-23", "2016-12-29","2017-02-08" ,"2017-07-24", "2017-07-26" ,"2018-04-05" ,"2018-06-01", "2019-02-07" ,"2019-03-25"), price = c(1300, 1300, 1300 ,1300 ,1300 ,1300 ,1300 ,1300 ,1300 ,1300 ,1375 ,1375 ,1405 ,1405))
summary(e)
When I use ts(), I am not sure why the values are changing. Could someone explain?
b <- ts(e$price, start = c(2016,3), end = c(2019,3), frequency = 12)
Output: Mar 2013 has the right price that is 1300 , but Mar 2019 has the wrong value, it has 1300 instead of 1405
How to fix this?
Your data does not represent a time series since mathematically a time series is a function from unique times to values. Also ts
does not handle daily data well.
If we omit all but the last data point in any year/month then we can convert it to a monthly ts
series by reading it into a zoo series and in doing so converting the date column to yearmon
class and aggregating on identical year/months using the last value. Then convert that to ts
class.
library(zoo)
z <- read.zoo(e, FUN = as.yearmon, aggregate = function(x) tail(x, 1))
as.ts(z)
giving:
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2016 1300 NA 1300 NA NA NA 1300 NA 1300 1300
2017 NA 1300 NA NA NA NA 1300 NA NA NA NA NA
2018 NA NA NA 1375 NA 1375 NA NA NA NA NA NA
2019 NA 1405 1405
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.