Open In App
Related Articles

Read contents of a CSV File in R Programming – read.csv() Function

Like Article
Save Article
Report issue

read.csv() function in R Language is used to read “comma separated value” files. It imports data in the form of a data frame.

Syntax: read.csv(file, header, sep, dec) Parameters: file: the path to the file containing the data to be imported into R. header: logical value. If TRUE, read.csv() assumes that your file has a header row, so row 1 is the name of each column. If that’s not the case, you can add the argument header = FALSE. sep: the field separator character dec: the character used in the file for decimal points.

The data variable will hold the contents of the CSV file once we have used the read.csv() function, though you are free to use another variable.

The read.csv() function also accepts a number of optional arguments that we can use to modify the import procedure. we can choose to treat the first row as column names, select the delimiter character, and more. For additional information on these options, consult the read.csv() documentation.

Ensure that the file is in the correct CSV format with the correct delimiters and quotation characters by giving the read.csv() method the correct file path or URL.

Example 1: Reading file from the same folder 


# R program to read a csv file
# Get content into a data frame
data <- read.csv("CSVFileExample.csv",
                header = FALSE, sep = "\t")
# Printing content of Text File



   V1 V2 V3
1 100 AB ab
2 200 CD cd
3 300 EF ef
4 400 GH gh
5 500 IJ ij

Example 2: Reading files from different directories 


# Simple R program to read csv file
x <- read.csv("D://Datas//myfile.csv")
# print x



  X  V1 V2 V3
1 1 100 a1 b1
2 2 200 a2 b2
3 3 300 a3 b3

Example 3: Reading a CSV file with a different delimiter


data <- read.csv("path/to/your/file.csv", sep = ";")


The sep option is set to “;” in this example, which indicates that the CSV file is utilizing the semicolon (;) as the delimiter rather than the standard comma (,).

Example 4: Treating the first row as column names


data <- read.csv("path/to/your/file.csv", header = TRUE)


The first row of the CSV file is handled as the column names by default because the header argument is set to TRUE. If the first row of our CSV file does not contain column names, we can import the data without them by setting header = FALSE.

Example 4: Specifying column classes


data <- read.csv("path/to/your/file.csv",
       colClasses = c("character", "numeric", "integer"))


We can define the classes for each column in the CSV file using the colClasses option. For the sake of this illustration, the first column will be interpreted as a character, the second as a number, and the third as an integer. When we want to manage the data types of particular columns.

Example 5: Skipping rows and specifying missing values


data <- read.csv("path/to/your/file.csv", skip = 3, na.strings = c("", "NA"))


We can skip a specific number of rows at the CSV file’s beginning by using the skip argument. The first three rows in this illustration will be omitted. The values that should be considered as missing values (NA) are specified by the an. strings argument. The string “NA” and empty strings are both recognized as missing values in this situation.

Last Updated : 05 Jul, 2023
Like Article
Save Article
Share your thoughts in the comments
Similar Reads