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
R Tutorial | Learn R Programming Language R is an interpreted programming language widely used for statistical computing, data analysis and visualization. R language is open-source with large community support. R provides structured approach to data manipulation, along with decent libraries and packages like Dplyr, Ggplot2, shiny, Janitor a
4 min read
R Programming Language - Introduction R is a programming language and software environment that has become the first choice for statistical computing and data analysis. Developed in the early 1990s by Ross Ihaka and Robert Gentleman, R was built to simplify complex data manipulation and create clear, customizable visualizations. Over ti
4 min read
R-Data Frames R Programming Language is an open-source programming language that is widely used as a statistical software and data analysis tool. Data Frames in R Language are generic data objects of R that are used to store tabular data. Data frames can also be interpreted as matrices where each column of a matr
6 min read
Read contents of a CSV File in R Programming - read.csv() Function 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, sele
3 min read
R-Data Types Data types in R define the kind of values that variables can hold. Choosing the right data type helps optimize memory usage and computation. Unlike some languages, R does not require explicit data type declarations while variables can change their type dynamically during execution.R Programming lang
5 min read
Data Visualization in R Data visualization is the practice of representing data through visual elements like graphs, charts, and maps. It helps in understanding large datasets more easily, making it possible to identify patterns and trends that support better decision-making. R is a language designed for statistical analys
5 min read
R-Matrices R-matrix is a two-dimensional arrangement of data in rows and columns. In a matrix, rows are the ones that run horizontally and columns are the ones that run vertically. In R programming, matrices are two-dimensional, homogeneous data structures. These are some examples of matrices:R - MatricesCreat
10 min read
apply(), lapply(), sapply(), and tapply() in R In this article, we will learn about the apply(), lapply(), sapply(), and tapply() functions in the R Programming Language. The apply() collection is a part of R essential package. This family of functions helps us to apply a certain function to a certain data frame, list, or vector and return the r
4 min read
Functions in R Programming A function accepts input arguments and produces the output by executing valid R commands that are inside the function. Functions are useful when we want to perform a certain task multiple times.In R Programming Language when we are creating a function the function name and the file in which we are c
5 min read
R - Bar Charts Bar charts provide an easy method of representing categorical data in the form of bars. The length or height of each bar represents the value of the category it represents. In R, bar charts are created using the function barplot(), and it can be applied both for vertical and horizontal charts.Syntax
4 min read