匯入資料
https://www.r-bloggers.com/read-excel-files-from-r/
讀入excel資料->excel轉成csv格式
Usage
read.table()
read.csv()
read.csv2()
read.delim()
read.delim2()
Example
df = read.table("myfile.csv", header = TRUE)
df <- read.csv(file="simple.csv",head=TRUE,sep=",")
df = read.table("clipboard")
summary(df)
轉碼
匯入 CSV 檔的時候會碰到一種比較特別的問題,就是作業系統編碼不同的問題,Windows 的中文編碼是 big5,而 Linux / Mac 都是 UTF-8,所以在 Linux / Mac 匯入來自於 Windows CSV 檔常常會發生亂碼,那該如何解決此問題,本人的做法是將資料讀進來轉成 UTF-8,在輸出一份 CSV 檔,以下先以一個 CSV 檔為主,加以調整修改就可以改成一次跑一個資料夾下的所有 CSV 檔。
df <- readLines("file.csv", encoding="big5") # 讀取實價登入資料,是一行一行讀取進來。
df <- iconv(df, "big5", "utf8") 將資料轉成 UTF-8。
tree <- read.csv(file="trees91.csv",header=TRUE,sep=","); attributes(tree) $names [1] "C" "N" "CHBR" "REP" "LFBM" "STBM" "RTBM" "LFNCC" [9] "STNCC" "RTNCC" "LFBCC" "STBCC" "RTBCC" "LFCACC" "STCACC" "RTCACC" [17] "LFKCC" "STKCC" "RTKCC" "LFMGCC" "STMGCC" "RTMGCC" "LFPCC" "STPCC" [25] "RTPCC" "LFSCC" "STSCC" "RTSCC"
$class [1] "data.frame"
$row.names [1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "15" [16] "16" "17" "18" "19" "20" "21" "22" "23" "24" "25" "26" "27" "28" "29" "30" [31] "31" "32" "33" "34" "35" "36" "37" "38" "39" "40" "41" "42" "43" "44" "45" [46] "46" "47" "48" "49" "50" "51" "52" "53" "54"