R Markdown

library(readxl) library(dplyr) library(tidyr) library(xts) library(knitr) library(ggplot2) library(anytime)

Set working directory to your TEJ export folder

setwd(“C:/TejPro/TejPro/DataExport”)

Helper function to safely read TEJ files (works for both .xlsx and .csv)

read_tej_data <- function(filename, label) { + # If file is Excel (.xlsx) + if (grepl(“\.xlsx$”, filename)) { + df <- read_excel(filename, skip = 1) + } else { + # If file is CSV exported with BIG5 encoding + df <- readr::read_csv(filename, skip = 1, locale = readr::locale(encoding = “BIG5”), show_col_types = FALSE) + } +
+ df_clean <- data.frame( + Date = anydate(df[[1]]), # Converts any date string format safely + Price = as.numeric(df[[5]]) # Col 5 = Close(NTD) / AdjClose + ) %>% + filter(!is.na(Date) & !is.na(Price)) +
+ colnames(df_clean)[2] <- label + return(df_clean) + }

1. Load data (change file extensions to .csv or .xlsx based on your actual files)

data_0050 <- read_tej_data(“0050.xlsx”, “0050 元大台灣50”) data_0052 <- read_tej_data(“0052.xlsx”, “0052 富邦科技”)
data_0056 <- read_tej_data(“0056.xlsx”, “0056 元大高股息”)

2. Merge into wide format

prices <- data_0050 %>% + inner_join(data_0052, by = “Date”) %>% + inner_join(data_0056, by = “Date”) %>% + arrange(Date) %>% + filter(Date >= as.Date(“2010-01-01”), Date <= as.Date(“2026-09-22”))

3. Create time series object safely

prices_df <- as.data.frame(prices)

price_xts <- xts( + x = as.matrix(prices_df[, -1]), + order.by = prices_df$Date + )

4. Check results

head(price_xts) 0050 元大台灣50 0052 富邦科技 0056 元大高股息 2010-01-04 8.4501 2.9216 8.4679 2010-01-05 8.4501 2.9256 8.4318 2010-01-06 8.6072 2.9936 8.5579 2010-01-07 8.5847 2.9616 8.4859 2010-01-08 8.6371 2.9528 8.5760 2010-01-11 8.6595 2.9696 8.6660 price_xts[“2020-01-02/2020-01-09”] 0050 元大台灣50 0052 富邦科技 0056 元大高股息 2020-01-02 19.8573 8.1422 16.9530 2020-01-03 19.8573 8.0922 17.0054 2020-01-06 19.6031 8.0033 16.8772 2020-01-07 19.5421 7.9755 16.7199 2020-01-08 19.4506 7.9144 16.6091 2020-01-09 19.7149 8.0477 16.7257