How to import data from excel pro R?

7

I have a table in excel that I need to import into R. I was able to do this quietly with the read.csv function when the table in question was online, but I'm having trouble importing the file directly from my computer. As the code I'm doing will be sent to someone else, I can not put an absolute path, and I'm having trouble applying the function to a relative path.

I think my problem is basic, but since I did not find any questions about it, I thank anyone who can answer me.

    
asked by anonymous 04.06.2014 / 06:26

2 answers

6

There are a few ways to do this. I could not understand the situation very well, but here are some options for reading Excel files:

With package gdata :

require(gdata)

df = read.xls ("myfile.xlsx"), sheet = 1, header = TRUE)

With the RODBC package '':

require(RODBC)

conn = odbcConnectExcel("myfile.xlsx") # Abre uma conexao com o arquivo excel

sqlTables(conn)$TABLE_NAME # mostra todas planilhas

df = sqlFetch(conn, "Sheet1") # le uma planilha

close(conn) # fecha a conexao com o arquivo

With package xlsx :

require(xlsx)

read.xlsx("myfile.xlsx", sheetName = "Sheet1")

If you have no problem with English, other methods can be seen at: link

    
04.06.2014 / 20:25
2

Currently, the best way to import from excel is by using the readxl package. Unlike the xlsx package, it does not depend on rJava , which makes it more portable and easy to install.

You can install the package using:

install.packages("readxl")

Examples of use:

library(readxl)

# Abre tanto arquivos xls como xlsx
read_excel("my-old-spreadsheet.xls")
read_excel("my-new-spreadsheet.xlsx")

# É possível especificar a aba tanto por um número quanto pelo nome
read_excel("my-spreadsheet.xls", sheet = "data")
read_excel("my-spreadsheet.xls", sheet = 2)

# Se valores omissos (NA's) forem representados por outro
# valor que não seja uma célula vazia.
read_excel("my-spreadsheet.xls", na = "NA")
    
17.10.2016 / 20:45