How to read CSV file into R

If you are using R much you will likely need to read in data at some point. While R can read excel .xls and .xlsx files these file types often cause problems. Comma separated files (.csv) are much easier to work with. It’s best to save these files as csv before reading them into R. If you need to read in a csv with R the best way to do it is with the command read.csv.

Here is an example of how to read CSV in R:

Step 1: Save Excel file as CSV file

Step 2: On R console type the following command fileToOpen<-read.csv(file.choose(), header=TRUE) 

The file.choose() command of R to open the file.

Here header is true because the CSV file has column headings in it

The above reads the file fileName.csv into a data frame that it creates called myData. header=TRUE specifies that this data includes a header row and sep=”,” specifies that the data is separated by commas (though read.csv implies the same I think it’s safer to be explicit) Also ensure that in file path you use the forward slash (“/”) instead of the usual backslash.

Cheers!

comments powered by Disqus