user9092800
user9092800

Reputation:

convert and na remove xts data by date

to convert data set into xts by date, and removing the date which contain NA, as follow:

data

tlkm_ts <- xts(tlkm[,-1], order.by = as.Date(tlkm[,1], "%Y-%m-%d"), na.rm = T)

xts data set to perform time series predicting

Upvotes: 0

Views: 145

Answers (1)

maraboule
maraboule

Reputation: 363

It is a bit difficult to know the type of your object tlkm but let's say it is a matrix

col1 <- c("2017-12-06", "2017-12-07", "2017-12-08", "2017-12-09", "2017-12-10", 
          "2017-12-11", "2017-12-12", "2017-12-13", "2017-12-14", "2017-12-15", 
          "2017-12-16", "2017-12-17", "2017-12-18", "2017-12-19", "2017-12-20", 
          "2017-12-21", "2017-12-22", "2017-12-27", "2017-12-28", "2017-12-29", 
          "2017-12-30", "2017-12-31", "2018-01-01")
col2 <- c(4200, 4200, 4140, NA, NA, 4140, 4170, 4200, 4250, 4230, NA, NA, 4240,
          4190, 4160, 4250, 4300, 4300, 4390, 4440, NA, NA, NA)
tlkm <- matrix(c(col1, col2), ncol = 2)

Then you probably want to use na.exclude() instead of the parameter na.rm=TRUE in the xts() function

tlkm_ts <- na.exclude(xts(as.numeric(tlkm[, -1]), order.by = as.Date(tlkm[, 1])))

which gives

> tlkm_ts
           [,1]
2017-12-06 4200
2017-12-07 4200
2017-12-08 4140
2017-12-11 4140
2017-12-12 4170
2017-12-13 4200
2017-12-14 4250
2017-12-15 4230
2017-12-18 4240
2017-12-19 4190
2017-12-20 4160
2017-12-21 4250
2017-12-22 4300
2017-12-27 4300
2017-12-28 4390
2017-12-29 4440

Hope that helps

Upvotes: 1

Related Questions