1.加載 readxl 包,利用 reade_excel() 函數
install.packages("readxl")
library(readxl)
data = read_excel("22_data.xlsx",sheet = 1)
read_excel函數的參數設置:
用法:
read.xlsx(xlsxFile, sheet = 1, startRow = 1, colNames = TRUE,
rowNames = FALSE, detectDates = FALSE, skipEmptyRows = TRUE,
skipEmptyCols = TRUE, rows = NULL, cols = NULL, check.names = FALSE,
namedRegion = NULL, na.strings = "NA", fillMergedCells = FALSE)
參數:
startRow :從這一行開始查找數據,無論startRow是多少,文件上面的空行都會被跳過
sheet : 從那一頁開始讀
colNames :如果為真,第一行的數據就是列的名字
rowNames :如果為真,第一類數據會被作為行的名字
detectDates :如果為真,則嘗試識別日期並進行轉換
skipEmptyRows 如果為真,會跳過空行,如果第一個有數據行之后有空行則返回一行NAs
If TRUE, empty rows are skipped else empty rows after the first row containing data will return a row of NAs.
skipEmptyCols 如果為真,會跳過空列
If TRUE, empty columns are skipped.
rows 如果為空則讀所有的行,否則輸入一個向量來讀取向量對應的行。
cols 輸入一個數值向量來指定讀表格中的那些列,如果為空的化,讀完所有的列。
check.names 邏輯變量,如果為真,則檢查數據框中變量的名稱,以確保它們是語法上有效的變量名
namedRegion 工作簿中的命名區域。 如果不是NULL,則 startRow,rows和cols參數將被忽略
na.strings 字符串的字符向量將會被解釋稱為 NA,空格將被返回為 NA 。
fillMergedCells 如果為TRUE,則合並單元格中的值將提供給合並中的所有單元格。
注意:此函數既可以讀 .xls 也可以讀.xlsx 類型文件
2.加載 openxlsx 包,利用 read.xlsx() 函數
install.packages("openxlsx")
library(xlsx)
read.xlsx("22_data.xlsx",sheet=1)
用法
read.xlsx(xlsxFile, sheet = 1, startRow = 1, colNames = TRUE,
rowNames = FALSE, detectDates = FALSE, skipEmptyRows = TRUE,
skipEmptyCols = TRUE, rows = NULL, cols = NULL, check.names = FALSE,
namedRegion = NULL, na.strings = "NA", fillMergedCells = FALSE)
參數設置
xlsxFile :一個xlsx文件,或者文件的網址
sheet :從那一頁開始讀
The name or index of the sheet to read data from.
注意:此函數僅可以讀取 .xlsx 類型文件
3.讀取文件
> library(openxlsx) #第一種方式 > read.xlsx("22_data.xlsx",sheet=1) t y X1 X2 X3 1 2000 12581.51 100280.1 15886.50 98.5 2 2001 15301.38 110863.1 18902.58 99.2 3 2002 17636.45 121717.4 22053.15 98.7 4 2003 20017.31 137422.0 24649.95 99.9 5 2004 24165.38 161840.2 28486.89 102.8 6 2005 28778.54 187318.9 33930.28 100.8 7 2006 34804.35 219438.5 40422.73 101.0 8 2007 45621.97 270232.3 49781.35 103.8 9 2008 54223.79 319515.5 62592.66 105.9 10 2009 59521.59 349081.4 76299.93 98.8 11 2010 73210.79 413030.3 89874.16 103.1 12 2011 89738.39 489300.6 109247.79 104.9 13 2012 100614.28 540367.4 125952.97 102.0 14 2013 110530.70 595244.4 140212.10 101.4 15 2014 119175.31 643974.0 151785.56 101.0 16 2015 124922.20 689052.1 175877.77 100.1 17 2016 130360.73 743585.5 187755.21 100.7
> library(readxl) #第二種方式 > data = read_excel("22_data.xlsx",sheet = 1) > data # A tibble: 17 x 5 t y X1 X2 X3 <dbl> <dbl> <dbl> <dbl> <dbl> 1 2000 12582. 100280. 15886. 98.5 2 2001 15301. 110863. 18903. 99.2 3 2002 17636. 121717. 22053. 98.7 4 2003 20017. 137422 24650. 99.9 5 2004 24165. 161840. 28487. 103. 6 2005 28779. 187319. 33930. 101. 7 2006 34804. 219438. 40423. 101 8 2007 45622. 270232. 49781. 104. 9 2008 54224. 319516. 62593. 106. 10 2009 59522. 349081. 76300. 98.8 11 2010 73211. 413030. 89874. 103. 12 2011 89738. 489301. 109248. 105. 13 2012 100614. 540367. 125953. 102 14 2013 110531. 595244. 140212. 101. 15 2014 119175. 643974 151786. 101 16 2015 124922. 689052. 175878. 100. 17 2016 130361. 743586. 187755. 101.
注意:對於同樣的數據,兩個函數的輸出結果並不一樣,當然理想的方式是上程序框的第一個函數。