Import Excel file to R

Asked

Viewed 188 times

1

I am having trouble importing an excel file into R. I am using readxl package, my working directory is set in the same location as excel file :

setwd("C:/R sidra")

However, when executing the readxl command, you do not find the file path:

library(read_excel)

read_excel("MUNICÍPIOS")

Error: path does not exist: ŋ MUNICÍPIOS'

4 answers

2

If your problem is specifying the path, try bringing it with the following function:

file.choose()

It will open the folder menu of your operating system. Take two clicks on the filing cabinet you want to import. The path of this file will be sent to the R Console. Something like this:

"/home/user/importR/my1/MeusDados.xlsx"

Copy this path ("/home/user/importR/my1/MeusDados.xlsx") without the file name. This way:

"/home/user/importR/my1"

Use it in the function below. So:

setwd("/home/user/importR/my1")

Try to import with the name complete of the archive ("/home/user/importR/my1/MeusDados.xlsx"):

library(readxl)

meus_dados <- read_excel("/home/user/importR/my1/MeusDados.xlsx")

1

Hello! I noticed that you did not put the file format at the end.

Let’s assume it’s .xlsx. Try it this way and see if it works:

library(readxl)

read_excel("MUNICÍPIOS.xlsx")
  • changed the command, but the error remains > read_excel("MUNICIPALITIES.xlxs") Error: path does not exist: ː MUNICÍPIOS.xlxs'

  • @Ingledseiévini, try to declare the whole directory. Example: df <- read_excel("c:/your/folder/your.xlsx file")

  • another thing, you wrote in the read_excel comment("MUNICIPALITIES.xlxs"). the correct is xlsx and not xlxs

  • I had not attempted the error, but corrected and the error persists > read_excel("C:/R cidra/MUNICIPIOS.xlsx") Error: path does not exist: pra C:/R cider/MUNICIPIOS.xlsx'

  • Ok. The error message says that the path does not exist. Is the directory really correct? Copy and paste the file, for example, into the "c:/file.xlsx" folder and try read_excel again ("c:/file.xlsx")

  • another thing: the R sometimes does not recognize this bar... try read_excel("c//file.xlsx") or read_excel("c file.xlsx"). Also check that the file format you are wanting to import is the same. (check by right clicking on the file and then on properties)

  • The file is saved in xls, I made a test as you told me read_excel("C: MUNICIPIOS.xlsx"), the result > read_excel("C: MUNICIPIOS.xlsx") Error: ' M' is an unrecognized escape in Character string Starting ""C: M"

  • If the file is saved in XLS then you should use: df <- read_excel("c:/file.xls"), otherwise it will not be found.

Show 4 more comments

1

The ideal is that you use a file without accentuation to avoid localization or encoding problems. Use in the format below.

library(readxl)

read_excel("MUNICIPIOS.xlsx")

Remember that the "xlsx" is the file format, if your Excel file is as a CSV change the format in the above method.

0

If it is an encoding problem, try to find the file through pattern in dir(). I put a pattern well general but you can replace it with something more specific.

file <- dir("C:/R sidra", pattern = "(?i)mun.*\\.xslx$", full.names = T)

dados <- readxl::read_xlsx(file)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.