# Load required libraries
library(tidyverse)
library(xts)
library(tidyverse)
library(xts)
library(lubridate)
# 1. Read raw CSV data from TEJ export
raw_data <- read_csv("C:/TejPro/TejPro/DataExport/tej_etf_data.csv")
# 3. Clean and rename columns
colnames(raw_data) <- c("Date", "0050 元大台灣50", "0052 富邦科技", "0056 元大高股息")
# 4. Format date column and sort chronologically (earliest first)х
formatted_df <- raw_data %>%
mutate(Date = ymd(Date)) %>%
filter(!is.na(Date)) %>%
arrange(Date)
# 5. Convert to xts time series object
ts_data <- xts(formatted_df[, -1], order.by = formatted_df$Date)
# 6. Final result
head(ts_data)
## 0050 元大台灣50 0052 富邦科技 0056 元大高股息
## 2025-08-18 52.2403 29.1179 31.8394
## 2025-08-19 52.1423 29.0064 31.7578
## 2025-08-20 50.6721 28.0165 31.2952
## 2025-08-21 50.9661 28.3093 31.4857
## 2025-08-22 50.7211 28.0932 31.3859
## 2025-08-25 51.6522 28.8251 31.6943
## 3. Display Time Series Output
# Display the first few rows of the formatted time series
head(ts_data)
## 0050 元大台灣50 0052 富邦科技 0056 元大高股息
## 2025-08-18 52.2403 29.1179 31.8394
## 2025-08-19 52.1423 29.0064 31.7578
## 2025-08-20 50.6721 28.0165 31.2952
## 2025-08-21 50.9661 28.3093 31.4857
## 2025-08-22 50.7211 28.0932 31.3859
## 2025-08-25 51.6522 28.8251 31.6943