How to Read CSV in 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 filetypes 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:
# Read CSV into R MyData <- read.csv(file="c:/TheDataIWantToReadIn.csv", header=TRUE, sep=",")
The above reads the file TheDataIWantToReadIn.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).
Note that the above includes the file path (the c:/). If you’ve already set a working directory in R you can just list the file, like this:
# Read CSV into R MyData <- read.csv(file="TheDataIWantToReadIn.csv", header=TRUE, sep=",")
There are other options that can be used with read.csv. See official R-manual page on read.csv to learn more: http://cran.r-project.org/doc/manuals/R-data.html.
Why not read.table?
I’ve found read.csv to be more reliable. I sometimes get errors with read.table. I’ll post the errors when I come across them again.
To practice reading csv data in R, try the exercises in this importing data in R tutorial.
Thanks for reading! This website took a great deal of time to create. If it was helpful to you, please show it by sharing with friends, liking, or tweeting! If you have any thoughts regarding this R code please post in the comments.
Hi Justin, thanks for sharing it. Can you tell me is there any syntax to create objects(DataFrame)
Ex: MyData (OR) My.Data
One way to create a data frame is to first create a matrix. This matrix is filled with NAs and has 5 rows of 3 variables:
MyData <- matrix(NA, 5, 3) Now convert the matrix to a data frame: MyData <- data.frame(MyData)
Your R how to’s have helped me so much with my R programming course – Pages are well written , clear and simple to comprehend. Thank you
You’re welcome! Good luck with R.
Thank you for the explanation,
I really appreciate that you took your time to do this.
You’re welcome! I’m happy to help.
how do you use the variable to access the data after reading from the file?
In the example above, we read a csv into R as an object called MyData. If you want to do something with MyData you refer to that object with your command. For example, if I want a summary of MyData I would use summary(MyData). If you want to do something with a specific variable in my data you use the object name, then the $ symbol, then the variable name. For example, if I want a summary of a variable called “VariableOne” in MyData, I would use summary(MyData$VariableOne).
I just want to read a file (.csv) using java with JRI api . i set all the path variable but it’s showing me Native library not found !
what should to do ?
I’m sorry, I don’t know how to fix this. This question on Stack Overflow seems related though: http://stackoverflow.com/questions/7496837/read-in-a-csv-file-in-r-from-java-using-jri
Thank you!!!!!! So helpful!
You’re welcome! Happy to help.
I would think that read.csv would actually be using read.table under the covers.