How to Import a CSV File into R ?
Last Updated :
23 Jul, 2025
A CSV file is used to store contents in a tabular-like format, which is organized in the form of rows and columns. The column values in each row are separated by a delimiter string. The CSV files can be loaded into the working space and worked using both in-built methods and external package imports.
Method 1: Using read.csv() method
The read.csv() method in base R is used to load a .csv file into the present script and work with it. The contents of the csv can be stored into the variable and further manipulated. Multiple files can also be accessed in different variables. The output is returned to the form of a data frame, where row numbers are assigned integers beginning with 1.
Syntax: read.csv(path, header = TRUE, sep = ",")
Arguments :
- path : The path of the file to be imported
- header : By default : TRUE . Indicator of whether to import column headings.
- sep = "," : The separator for the values in each row.
Code:
R
# specifying the path
path <- "/Users/mallikagupta/Desktop/gfg.csv"
# reading contents of csv file
content <- read.csv(path)
# contents of the csv file
print (content)
Output:
ID Name Post Age
1 5 H CA 67
2 6 K SDE 39
3 7 Z Admin 28
In case, the header is set to FALSE, the column names are ignored, and default variables names are displayed for each column beginning from V1.
R
path <- "/Users/mallikagupta/Desktop/gfg.csv"
# reading contents of csv file
content <- read.csv(path, header = FALSE)
# contents of the csv file
print (content)
Output:
V1 V2 V3 V4
1 5 H CA 67
2 6 K SDE 39
3 7 Z Admin 28
Method 2: Using read_csv() method
The "readr" package in R is used to read large flat files into the working space with increase speed and efficiency.
install.packages("readr")
The read_csv() method reads a csv file reading one line at a time. The data using this method is read in the form of a tibble, of the same dimensions as of the table stored in the .csv file. Only ten rows of the tibble are displayed on the screen and rest are available after expanding, which increases the readability of the large files. This method is more efficient since it returns more information about the column types. It also displays progress tracker for the percentage of file read into the system currently if the progress argument is enabled, therefore being more robust. This method is also faster in comparison to the base R read.csv() method.
Syntax: read_csv (file-path , col_names , n_max , col_types , progress )
Arguments :
- file-path : The path of the file to be imported
- col_names : By default, it is TRUE. If FALSE, the column names are ignored.
- n_max : The maximum number of rows to read.
- col_types : If any column succumbs to NULL, then the col_types can be specified in a compact string format.
- progress : A progress meter to analyse the percentage of file read into the system
Code:
R
library("readr")
# specifying the path
path <- "/Users/mallikagupta/Desktop/gfg.csv"
# reading contents of csv file
content <- read_csv(path, col_names = TRUE)
# contents of the csv file
print (content)
Output:
Similar Reads
How to import an Excel File into R ? In this article, we will discuss how to import an excel file in the R Programming Language. There two different types of approaches to import the excel file into the R programming language and those are discussed properly below. File in use: Method 1: Using read_excel() In this approach to import th
3 min read
How to Import .dta Files into R? In this article, we will discuss how to import .dta files in the R Programming Language.There are many types of files that contain datasets, for example, CSV, Excel file, etc. These are used extensively with the R Language to import or export data sets into files. One such format is DAT which is sav
2 min read
How to Import TSV Files into R In this article, we are going to discuss how to import tsv files in R Programming Language. The TSV is an acronym for Tab Separated Values, in R these types of files can be imported using two methods one is by using functions present in readr package and another method is to import the tsv file by
2 min read
How to Import SAS Files into R? In this article, we are going to see how to import SAS files(.sas7bdat) into R Programming Language. SAS stands for Statistical Analysis Software, it contains SAS program code saved in a propriety binary format. The R packages discussed, haven and sas7bdat, involved reverse engineering this proprie
1 min read
How to Import SPSS Files into R? In this article, we are going to see how to import SPSS Files(.sav files) into R Programming Language. Used file: Click Method 1: Using haven Package Here we will use the haven package to import the SAS files. To install the package: install.packages('haven') To import the SAV file read_sav() method
1 min read
How to import an Excel file into Rmarkdown? In this article, we are going to learn how to import excel file into Rmarkdown in R programming language. Excel file is a file where the data is stored in the form of rows and columns. The R markdown is a file where we can embed the code and its documentation in the same file and are able to knit it
2 min read