Late to the party, but the tsbox package is designed to perform conversions like this. To convert your data into a ts
-object, you can do:
dta <- data.frame(
Dates = c("3/14/2013", "3/15/2013", "3/18/2013", "3/19/2013"),
Bajaj_close = c(1854.8, 1850.3, 1812.1, 1835.9),
Hero_close = c(1669.1, 1684.45, 1690.5, 1645.6)
)
dta
#> Dates Bajaj_close Hero_close
#> 1 3/14/2013 1854.8 1669.10
#> 2 3/15/2013 1850.3 1684.45
#> 3 3/18/2013 1812.1 1690.50
#> 4 3/19/2013 1835.9 1645.60
library(tsbox)
ts_ts(ts_long(dta))
#> Time Series:
#> Start = 2013.1971293045
#> End = 2013.21081883954
#> Frequency = 365.2425
#> Bajaj_close Hero_close
#> 2013.197 1854.8 1669.10
#> 2013.200 1850.3 1684.45
#> 2013.203 NA NA
#> 2013.205 NA NA
#> 2013.208 1812.1 1690.50
#> 2013.211 1835.9 1645.60
It automatically parses the dates, detects the frequency and makes the missing values at the weekends explicit. With ts_<class>
, you can convert the data to any other time series class.