YouTip LogoYouTip

R Func Read Csv

# R read.csv() Function - Read CSV Files [![Image 3: R Language Examples](#) R Language Examples](#) The R read.csv() function is used to read CSV (Comma-Separated Values) files into data frames. It is the first step in data analysis, and CSV is the most common data exchange format. The syntax of the read.csv() function is as follows: read.csv(file, header = TRUE, sep = ",", stringsAsFactors = FALSE) **Parameter Description:** * **file** File path or URL. * **header** Whether the first row is used as column names, default is TRUE. * **sep** Separator, default is comma for csv. read.csv2() uses semicolon. * **stringsAsFactors** Whether to convert strings to factors, default is FALSE since R 4.0. ## Examples # First create a sample CSV file df<-data.frame( Name =c("Zhang San", "Li Si", "Wang Wu"), Age =c(25, 30, 28), Score =c(88, 92, 76) ) write.csv(df, "temp_students.csv", row.names= FALSE) # Read CSV file data<-read.csv("temp_students.csv") print("Read CSV Data:") print(data) # View structure print("Data Types:") print(str(data)) # Specify options when reading data2 <-read.csv("temp_students.csv", stringsAsFactors = FALSE) print("Convert Strings to Factors:") print(str(data2)) The output of the above code execution is: "Read CSV Data:" Name Age Score1 Zhang San 25 882 Li Si 30 923 Wang Wu 28 76 [![Image 4: R Language Examples](#) R Language Examples](#)
← R Func RepR Func Rbind Cbind β†’