Read contents of a CSV File in R Programming - read.csv() Function
Last Updated :
22 Apr, 2025
read.csv() function in R Language is used to read "comma separated value" files. It imports data in the form of a data frame. 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.
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.
Example 1: Reading File From the Same Folder
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. 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. We will be using this CSV file .
R
data <- read.csv("CSVFileExample.csv",
header = FALSE, sep = "\t")
head(data)
Output:
Output Example 2: Reading Files From Different Directories
R
x <- read.csv("D://Datas//myfile.csv")
head(x)
Output:
Output Example 3: Reading a CSV File with a Different Delimiter
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 (,).
R
data <- read.csv("path/to/your/file.csv", sep = ";")
head(data)
Output:
OutputExample 4: Treating the First Row as Column Names
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.
R
data <- read.csv("path/to/your/file.csv", header = TRUE)
head(data)
Output:
OutputExample 5: Specifying Column Classes
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.
R
data <- read.csv("path/to/your/file.csv",
colClasses = c("character", "numeric"))
head(data)
Output:
OutputExample 6: Skipping Rows and Specifying Missing Values
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.
R
data <- read.csv("path/to/your/file.csv", skip = 3, na.strings = c("", "NA"))
head(data)
Output:
OutputIn this article, we learned how to use the read.csv()
function in R with various options to read and customize CSV file imports for effective data handling.
Similar Reads
Reading contents of a Text File in R Programming - read.table() Function The read.table() function in R can be used to read a text file's contents. A versatile and often used function for reading tabular data from different file formats, including text files, is read.table(). It returns the data in the form of a table.Syntax: read.table(filename, header = FALSE, sep = ""
2 min read
Read Lines from a File in R Programming - readLines() Function readLines() function in R Language reads text lines from an input file. The readLines() function is perfect for text files since it reads the text line by line and creates character objects for each of the lines. Syntax: readLines(path) Parameter: path: path of the file Example 1: Python3 # R progra
2 min read
Scan and Read Data from a File in R Programming - scan() Function scan() function in R Language is used to scan and read data. It is usually used to read data into vector or list or from file in R Language. Syntax: scan("data.txt", what = "character") Parameter: data.txt: Text file to be scanned Returns: Scanned output Example 1: Scanning of Text r # R Program to
3 min read
Reading Tabular Data from files in R Programming Often, the data which is to be read and worked upon is already stored in a file but is present outside the R environment. Hence, importing data into R is a mandatory task in such circumstances. The formats which are supported by R are CSV, JSON, Excel, Text, XML, etc. The majority of times, the data
4 min read
Reading Files in R Programming So far the operations using the R program are done on a prompt/terminal which is not stored anywhere. But in the software industry, most of the programs are written to store the information fetched from the program. One such way is to store the fetched information in a file. So the two most common o
9 min read
Working with CSV files in R Programming CSV (Comma-Separated Values) files are plain text files where each row contains data values separated by commas or other delimiters such as tabs. These files are commonly used for storing tabular data and can be easily imported and manipulated in R. We will explore how to efficiently work with CSV f
3 min read
How To Import Data from a File in R Programming The collection of facts is known as data. Data can be in different forms. To analyze data using R programming Language, data should be first imported in R which can be in different formats like txt, CSV, or any other delimiter-separated files. After importing data then manipulate, analyze, and repor
4 min read
How to Convert a CSV File to Microsoft Excel in R CSV refers to Comma-Separated Values. It holds plain text as a series of values (cells) separated by commas (, ) in a series of lines (rows). CSV file can actually open in a text editor and read it. On the other hand, Excel is used to display the data in horizontal and vertical rows. The data are u
2 min read
File Handling in R Programming In R Programming, handling of files such as reading and writing files can be done by using in-built functions present in R base package. In this article, let us discuss reading and writing of CSV files, creating a file, renaming a file, check the existence of the file, listing all files in the worki
4 min read
How to Write Multiple Excel Files From Column Values - R programming A data frame is a cell-based structure comprising rows and columns belonging to the same or different data types. Each cell in the data frame is associated with a unique value, either a definite value or a missing value, indicated by NA. The data frame structure is in complete accordance with the Ex
6 min read