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.

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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) 

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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 Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.