Read and write csv files in R
The CSV (Comma Separated Value) file is a plain text file that uses a comma to separate values.
R has a built-in functionality that makes it easy to read and write a CSV file.
Sample CSV File
To demonstrate how we read CSV files in R, let's suppose we have a CSV file named stud.csv with following data:
rollno name place mark
1 101 binu ernkulam 45
2 103 ashik alleppey 35
3 102 faisal kollam 48
4 105 biju kotayam 25
5 106 ann thrisur 30
The csv file contains rollnumber,name,place and mark of students
Read a CSV File in R
In R, we use the read.csv() function to read a CSV file available in our current directory. For example,
stud=read.csv("stud.csv")
print(stud)
output:
rollno name place mark
1 101 binu ernkulam 45
2 103 ashik alleppey 35
3 102 faisal kollam 48
4 105 biju kotayam 25
5 106 ann thrisur 30
Note: If the file is in some other location, we have to specify the path along with the file name as: read.csv("D:/folder/stud.csv")
We use the ncol() and nrow() function to get the total number of rows and columns present in the CSV file in R. For example,
# read stud.csv file from our directory
stud <- read.csv("stud.csv")
# print total number of columns
cat("Total Columns: ", ncol(stud))
# print total number of rows
cat("Total Rows:", nrow(stud))
Output
Output
Total Columns: 4
Total Rows: 5
In the above example, we have used the ncol() and nrow() function to find the total number of columns and rows in the stud.csv file.
In the above example, we have used the ncol() and nrow() function to find the total number of columns and rows in the stud.csv file.
In R, we can also find minimum and maximum data in a certain column of a CSV file using the min() and max() function. For example,
# read stud.csv file from our directory
stud <- read.csv("stud.csv")
# return minimum mark
minmark <- min(stud$mark)
# return maximum mark
maxmark<- max(stud$mark)
print(paste("Max Mark=",maxmark))
print(paste("Minimum Mark=",minmark))
Output:
[1] "Max Mark= 48"
[1] "Minimum Mark= 25"
Subset of a CSV File in R
In R, we use the subset() function to return all the data from a CSV file that satisfies the specified condition. For example,
# read stud.csv file from our directory
stud <- read.csv("stud.csv")
#mark >40
studnew=subset(stud,mark>40)
print(studnew)
Output:
rollno name place mark
1 101 binu ernkulam 45
3 102 faisal kollam 48
In the above example, we have specified a certain condition inside the subset() function to extract data from a CSV file.Here, subset() creates a subset of stud.csv with data column mark having data greater than 40 marks and stored it in the studnew data frame.
In R, we use the write.csv() function to write into a CSV file. We pass the data in the form of dataframe. For example,
# Create a data frame
dataframe <- data.frame (
RollNo=c(101,102,103),
Name = c("Juan", "Alcaraz", "Simantha"),
Place = c('Alleppey','cochin','Trivandrum'),
Mark = c(45,35,47))
# write dataframe into stud1 csv file
write.csv(dataframe, "stud1.csv")
In the above example, we have used the write.csv() function to export a data frame named dataframe to a CSV file. Notice the arguments passed inside write.csv(),
write.csv(dataframe, "stud1.csv")
Here,
dataframe\ - name of the data frame we want to export
stud1.csv - name of the csv file
Finally, the stud1.csv file is written in our directory. Open the file and check the output
stud1.csv - name of the csv file
Finally, the stud1.csv file is written in our directory. Open the file and check the output
write.csv(dataframe, "stud1.csv",
quote = FALSE
)
Comments
Post a Comment