1. Save Excel files into text
Saving Excel files into CSV can be done directly from Excel or through some external tools that allows batch operations. Native R functions for text data import can so be used.
- df = read.table("myfile.csv", header = TRUE)
复制代码2. Copy and paste from Excel to R
This is a fast solutions, but it has one main drawbacks: it requires to open Excel file, select data and copy. By the way, this is the best compromise when you're in a hurry.
- df = read.table("clipboard")
复制代码3. ODBC connection
- require(RODBC)
- conn = odbcConnectExcel("myfile.xlsx") # open a connection to the Excel file
- sqlTables(conn)$TABLE_NAME # show all sheets
- df = sqlFetch(conn, "Sheet1") # read a sheet
- df = sqlQuery(conn, "select * from [Sheet1 $]") # read a sheet (alternative SQL sintax)
- close(conn) # close the connection to the file
复制代码4. gdata package
- require(gdata)
- df = read.xls ("myfile.xlsx"), sheet = 1, header = TRUE)
复制代码5. xlsReadWrite package
- require(xlsReadWrite)
- xls.getshlib()
- df = read.xls("myfile.xls", sheet = 1)
复制代码6. XLConnect package
- require(XLConnect)
- wb = loadWorkbook("myfile.xlsx")
- df = readWorksheet(wb, sheet = "Sheet1", header = TRUE)
复制代码7.xlsx package
- require(xlsx)
- read.xlsx("myfile.xlsx", sheetName = "Sheet1")
- read.xlsx2("myfile.xlsx", sheetName = "Sheet1")
复制代码