In this tutorial you will learn how to read a csv file in R Programming with "read.csv" and "read.csv2" functions. You will learn to import data in R from your computer or from a source on internet using url for reading csv data.
1. Read a file from current working directory - using setwd.
2. Read a file from any location on your computer using file path.
3. Use file.choose() method to select a csv file to load in R.
4. Use full url to read a csv file from internet.
CSV stands for Comma Seperated Values. A CSV file is used to store data. It is a plain text file with .csv extension. In these type of files values are seperated by ',' (comma) or ';' (semi-colon)
If you want to learn R from start with simple and practical examples and go up to advanced level for data science, statistics or data analytics I will suggest you to see this simple and to the point Beginners Zero to Hero R Programming Complete Bootcamp: Learn R for Data Science with 11 Real World Applications.
CSV files have many benefits, as they are simple text files consisting of lines and each line of data is represented by a line in csv file which helps for storing tabular data. Most applications support reading and writing csv format.
An example of csv file isName,Age,Salary
The function read.csv() is used to import data from a csv file. This function can take many arguments, but the most important is file which is the name of file to be read. This function reads the data as a dataframe. If the values are seperated by a comma use read.csv() and if the values are seperated by ; (a semi-colon) use read.csv2() function. Otherwise there is no difference between these two functions.
In case you have a folder with many csv files and want to read from this folder quite often then it is better to first set that folder as your current working directory so that you can easily read files of this folder. For that purpose first you will need to use getwd() function and then use setwd() function. Lets say we want to make csvfiles folder on C: drive as our current working directory. We find our current working directory
 "d:/Program Files/RStudio"
Then we set our working directory to csvfiles folder on c: drive
Checking again for working directory
Now its time to read the file testfile.csv> data <- read.csv('testfile.csv')
Here data is a new variable or object which will store values read from csv file. read.csv is the name of function and we are providing only one argument to this function which is the file name with extension. After importing data in R you can check and see it with some common functions.
1. View(): This function will show you the values of csv file in a table format.
2. nrow(): This function returns the total number of rows in your dataframe.
3. ncol(): Returns the total number of columns in your dataframe.
4. colnames(): This function returns the column headers or column names.
5. str(): Returns the structure of your dataframe. Column names with data types and factors.
If you have to read a single csv file or you don't want to change your working directory then instead of using getwd and setwd for setting it, simply use file path for reading that file. Lets suppose your current working directory is "d:/Program Files/RStudio". And you simply want to read csv file without changing it. First you will create a new variable file and assign the complete path of file with its name and extension to this variable. And then use it to import data in R.
> file <- "c:/csvfiles/testfile.csv"
> data <- read.csv(file)
In case you don't exactly know the file location or even not sure about name of file you may simply use file.choose option in read.csv function. This will open a file dialog box to select the file you want to open in R.> data <- read.csv(file.choose())
To read a csv file from a web resource for data analysis the same function i.e read.csv() will be used. In this case you need to have a complete url or internet location of csv file. Lets support we want to read a file named advertising.csv from a website with this url "http://faculty.marshall.usc.edu/gareth-james/ISL/Advertising.csv"
This is a sample file which contains four columns and about 200 rows. You can import it in R and use the analysis methods describe earlier to have a view of this file contents. Then you may simply download this file on your computer and use the earlier methods to open it as a practice for reinforcing what you learnt in this tutorial.
To read file from that location R code will be
> data <- read.csv("http://faculty.marshall.usc.edu/gareth-james/ISL/Advertising.csv")
or you can use the file variable for storing url and then using it to import file in R
> file <- "http://faculty.marshall.usc.edu/gareth-james/ISL/Advertising.csv"
> data <- read.csv(file)