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") data
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") is.data.frame(data) ncol(data) nrow(data)
 TRUE  2  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 = ",") print(read_csv)
Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language.