Arrange data in Excel to open as table in R?

Question:

I have a file with 50 observations and 3 variables in an Excel file (.xlsx), and I transformed it to .csv. I used banco1<- read.csv("teste1.csv") to create the table in R, however my 3 variables appear in the same column. How do I make them appear separate?

Answer:

From the result you've observed, I'll assume you're using a Portuguese version of Excel on Windows. Generally, when we save a Windows Excel spreadsheet in CSV, it is saved with ; as a separator. This happens mainly in Brazilian versions (some European ones too) of Excel, because our decimal separator is the comma, and not the period, as it is in the United States.

In R this is already, in a way, anticipated by the read.csv2 function (see here ), which has the configuration of headers, decimals and separators that Excel on Windows uses by default. See in the function definition, doing ?read.csv

read.csv(file, header = TRUE, sep = ",", quote = "\"",
         dec = ".", fill = TRUE, comment.char = "", ...)

read.csv2(file, header = TRUE, sep = ";", quote = "\"",
          dec = ",", fill = TRUE, comment.char = "", ...)

In addition to this possible solution, as Anthony said, there are packages that make it easier to read files directly from Excel, such as the xlsx and gdata packages. See here and here .

Scroll to Top