R is a statistical language, and you often came across the external dataset built on text files. In R, we can read data from files stored outside the R environment. One of the easiest and most reliable ways of getting data into R is to use text files, in particular CSV (comma-separated values) files.
The CSV file format uses commas to separate the different elements in a line, and each line of data is in its own line in the text file, which makes CSV files ideal for representing tabular data.
How to Read CSV File in R
To read a CSV file in R, use the read.csv() function. The read.csv() function is used to read the CSV file available in your current working directory. You can check which directory the R workspace is pointing to using the getwd() function. You can also set a new working directory using the setwd() function.
read.csv("path to csv file")
The read.csv() function takes a csv file or path to the csv file. The read.csv() function has several arguments, but the only essential argument is a file, which specifies the location and filename.
data <- read.csv("cuisine.csv") data
placeID Rcuisine 910 132006 Dutch-Belgian 911 132005 French 912 132005 Seafood 913 132004 Seafood 914 132003 International 915 132002 Seafood 916 132001 Dutch-Belgian
By default, the read.csv() function gives the output as a data frame. This can be easily checked as follows. Also, we can check the number of columns and rows.
To check if the output is data frame, use the is.data.frame() function.
To get the number of columns of the data frame, use the ncol() function.
To get the number of rows of the data frame, use the nrow() function.
data <- read.csv("cuisine.csv") is.data.frame(data) ncol(data) nrow(data)
 TRUE  2  916
That is it for reading the csv file in the R language.
Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has written many programming blogs, which showcases his vast expertise in this field.