How to Read CSV File into DataFrame in R

To read a csv file into a data frame in R, you can use the “read.csv()” function, which loads the data from the CSV file into DataFrame. CSV file format is the easiest way to store scientific, analytical, or structured data (two-dimensional with rows and columns).


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

Example 1: How to use the read.csv() function in R

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 delimited in R, you can use the “sep” argument in the read.csv() function. 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 = ",")


That’s it.

See also

write.csv() in R

Leave a Comment