ts object to data frame
# Step 1: Create a time series object (ts object)
ts_data <- ts(c(1, 2, 3, 4, 5), start = c(2020, 1), frequency = 1)
# Step 2: Convert the ts object to a data frame
df_data <- data.frame(Time = time(ts_data), Value = as.vector(ts_data))
Explanation:
1. We create a time series object (ts_data
) using the ts
function. The time series data is provided as a vector c(1, 2, 3, 4, 5)
, and we specify the start time (start = c(2020, 1)
) and frequency (frequency = 1
).
- We convert the time series object to a data frame (
df_data
). Thetime
function extracts the time index from the ts object, andas.vector
is used to convert the ts object to a vector. We create a data frame with two columns: "Time" for the time index and "Value" for the values in the time series.