Skip to content
Related Articles

Related Articles

Improve Article

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

  • Last Updated : 19 Jun, 2020

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.

Example 1: Reading file from 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 
print(data) 

Output:

   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 file from different directory




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

Output:

  X  V1 V2 V3
1 1 100 a1 b1
2 2 200 a2 b2
3 3 300 a3 b3
My Personal Notes arrow_drop_up
Recommended Articles
Page :