CSV stands for Comma Separated Values file, which is a plain text file that contains a list of data. CSV file in R is used for exchanging the data between different applications. Like contact managers and databases mostly support CSV files.
Getting and Setting the Working Directory
The getwd() function is used to check which directory the R workspace is pointing to. A new working directory can be set by using the setwd() function.
The output will be based on the user’s current working directory and operating system.
Input as CSV File
The CSV file is a text file in which the values in the columns are separated by a comma.
Have a look at the below example in which data is present in the file named data.csv.
Reading a CSV File
The mentioned below is an example of the read.csv() function to read a CSV file available in your current working directory.
Output:
Analyzing a CSV file
The read.csv() function is the default function that gives the CSV file output as a data frame. To check the total rows and columns in a CSV file we used the following functions.
Output:
We can apply different functions on CSV files as below:
Get the student details with the highest marks
Output:
The students from the CS department
Output:
Writing into a CSV File
We can create a CSV file in R using the existing data frame. To create a CSV file in R we used the write.csv() function.