install.packages('pacman')
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.4'
## (as 'lib' is unspecified)
# Check the current working directory
current_dir <- getwd()
cat("Current working directory:", current_dir, "\n")
## Current working directory: /cloud/project
# List files in the current directory to confirm the file is present
cat("Files in the current directory:\n")
## Files in the current directory:
print(list.files())
## [1] "batjav 112035156.Rmd"            "batjav-112035156.Rmd"           
## [3] "project.Rproj"                   "tw0056_20070101_20191231.csv"   
## [5] "tw0056_20070101_20191231(1).txt"
# Correct the file path if needed
file_path <- "tw0056_20070101_20191231.csv"  # Update with your file name

# Check if the file exists in the current working directory
if (!file.exists(file_path)) {
  stop("The specified file does not exist. Please check the file path and try again.")
}
# Try reading the file again after resolving file path issues
etf56 <- read.csv(file_path, header = TRUE, stringsAsFactors = FALSE)

# Check for any errors
head(etf56)  # This should work without errors
##   證券代碼          簡稱   年月日 開盤價.元. 最高價.元. 最低價.元. 收盤價.元.
## 1       56 元大高股息    20071226    14.2060    14.2626    14.1211    14.2343
## 2       56 元大高股息    20071227    14.2343    14.4776    14.2343    14.3927
## 3       56 元大高股息    20071228    14.4493    14.5229    14.3758    14.4890
## 4       56 元大高股息    20071231    14.5456    14.7436    14.5456    14.7380
## 5       56 元大高股息    20080102    14.7154    14.8002    14.5456    14.5739
## 6       56 元大高股息    20080103    14.3644    14.4041    14.2909    14.3758
##   成交量.千股.
## 1         2354
## 2        10964
## 3         9327
## 4         3440
## 5         3824
## 6         3152
# Example for handling encoding
etf56 <- read.csv(file_path, header = TRUE, stringsAsFactors = FALSE, fileEncoding = "UTF-8")
etf56 <- etf56[c(3, 7)]
colnames(etf56) <- c("date", "price")
head(etf56)
##       date   price
## 1 20071226 14.2343
## 2 20071227 14.3927
## 3 20071228 14.4890
## 4 20071231 14.7380
## 5 20080102 14.5739
## 6 20080103 14.3758
tail(etf56)
##          date price
## 2964 20191224 28.74
## 2965 20191225 28.83
## 2966 20191226 28.86
## 2967 20191227 28.90
## 2968 20191230 29.00
## 2969 20191231 28.97
str(etf56)
## 'data.frame':    2969 obs. of  2 variables:
##  $ date : int  20071226 20071227 20071228 20071231 20080102 20080103 20080104 20080107 20080108 20080109 ...
##  $ price: num  14.2 14.4 14.5 14.7 14.6 ...
# 檢查是否有na 值
sum(is.na(etf56$price))
## [1] 0