How to Read CSV File into DataFrame in R

To read a csv file into a data frame in R, use the read.csv() function, which loads the data from the CSV file into DataFrame.


read.csv(file, header = TRUE, sep = ",", quote = "\"",
         dec = ".", fill = TRUE, comment.char = "", …)

Example 1: Usage of read.csv() function

data <- read.csv("cuisine.csv")


    placeID  Rcuisine
910 132006   Dutch-Belgian
911 132005   French
912 132005   Seafood
913 132004   Seafood
914 132003   International
915 132002   Seafood
916 132001   Dutch-Belgian

By default, the read.csv() function gives the output as a data frame. This can be easily checked as follows. Also, we can check the number of columns and rows.

data <- read.csv("cuisine.csv")


[1] TRUE
[1] 2
[1] 916

Example 2: Read CSV with custom delimiter using the “sep” argument

To read a CSV file with a custom delimiter, pass the “sep” argument. For example, if your file has data separated by a pipe (|), you can use sep=’|’. If your file has data separated by a tab (\t), you can use sep=’\t’.

read_csv <- read.csv("new_file.csv", sep = ",")


Leave a Comment