Read in a Dataset Directly as an Object of Class "ts" or "mts"
Read in a Dataset Directly as an Object of Class "ts" or "mts"
Allows the user to read in a data file directly as a "ts" or "mts" object, where a time point column in the data file is immediately used to set starting points and frequency of the time series automatically correctly. Works for equidistant observation time points, e.g. quarterly or monthly observations.
file: a data file name given as a string (including the file ending); the file should have at least two columns: one time column and at least one or multiple columns for time series observations; alternatively, a data frame can be passed to this argument.
time_column: a number that indicates which column in the dataset is the variable with the time points; by default, the first column is assumed to contain the information on time points.
sep: the separation symbol between the dataset columns.
dec: the decimal symbol in the dataset.
header: TRUE or FALSE; does the dataset have a row with headers at the beginning?
time_format: with the default NULL, standard date formats will be tried to transform the time points column into a date object; if the formatting of the time column is unusual, the formatting can be specified here as a string.
Returns
An object of class "ts" or "mts" is returned.
Details
The data file is internally read into R as a "zoo" object and then transformed into a "ts" object using zoo_to_ts. This happens without the user noticing. The result is an immediate transformation of the input data into an object of class "ts" or "mts" for the user.
Examples
### Create an example data filea <-1:12b <-21:32tp <- seq(from = as.Date("2020-01-01"), to = as.Date("2020-12-01"), by ="month")df <- data.frame( Time = tp, a = a, b = b
)file <- file.path(tempdir(),"ExampleFile.csv")write.table(df, file = file, quote =FALSE, sep =",", row.names =FALSE, col.names =TRUE)### Use the function to read in the dataxt <- read_ts(file)xt