How to Import a CSV File into R ? - GeeksforGeeks

23 May, 2021
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 pathpath <- "/Users/mallikagupta/Desktop/gfg.csv"  # reading contents of csv filecontent <- read.csv(path)# contents of the csv fileprint (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 filecontent <- read.csv(path, header = FALSE)  # contents of the csv fileprint (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 pathpath <- "/Users/mallikagupta/Desktop/gfg.csv"  # reading contents of csv filecontent <- read_csv(path, col_names = TRUE)  # contents of the csv fileprint (content)
Output:
Picked
R-CSV
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
How to Split Column Into Multiple Columns in R DataFrame?
Replace Specific Characters in String in R
How to filter R DataFrame by values in a column?
How to import an Excel File into R ?
R - if statement
Time Series Analysis in R
Plot mean and standard deviation using ggplot2 in R