Loading, please wait...

A to Z Full Forms and Acronyms

How to read and write the CSV files in R Language?

Jun 24, 2021 #RLanguage #Programming, 417 Views
In this article, you will learn about the read and writing in the CSV file.

How to read and write the CSV files in R Language?

In the R programming language, we can read data from the files and write data into the files. We can write the data into the files which, will be stored and accessed by the operating system. CSV, Excel, XML, etc are different file formats in which we can read and write files in R Language.

Getting and setting the directory 

To check the directory in which you are working can be done with the help of the getwd() function. To set the new directory setwd() function is used.

# Print current working directory.

print(getwd())

# Set current working directory.

setwd("/web/com")

# Print current working directory.

print(getwd())

When we execute the above code, it produces the following result −

[1] "/dic/com/1441086124_2016"

[1] "/dic/com"

Input as CSV file

The CSV is the text file that carries the value separated by a comma. The user can create the file CSV file by using the notepad. You can also copy and paste the data in the notepad to create the CSV file. Save the file by using "." and the CSV extension. 

Example: file name is reading.csv

id,name,salary,start_date,dept

1,John,623.3,2012-01-01,IT

2,Dane,515.2,2013-09-23,Operations

3,Michel,611,2014-11-15,IT

4,Rai,729,2014-06-11,HR

5,Jane,843.25,2015-03-27,Finance

6,Nio,578,2013-05-21,HR

7,Phib,722.5,2014-06-17,Finance

Reading the CSV File

The R language has an in-built read.csv() function to read the CSV file. It will show the CSV file of the working directory. 

info <- read.csv("reading.csv") ## 

print(info) // It will print the data of the csv file.

When executing the code, it will show.

      id, name, salary, start_date, dept

1 1 John 623.30 2012-01-01 IT

2 2 Dane 515.20 2013-09-23 Operations

3 3 Michel 611.00 2014-11-15 IT

4 4 Rai 729.00 2014-05-11 HR

5 5 Jane 843.25 2015-03-27 Finance

6 6 Nio 578.00 2013-05-21 HR

7 7 Phib 722.50 2014-06-17 Finance

 

Analyzing the CSV File

read.csv() function gives output in the data frame. We can check the number of columns and rows also. 

info <- read.csv("reading.csv")

print(is.data.frame(info)) #Checks the form of the output

print(ncol(info)) #print the number of columns

print(nrow(info)) #print the number of rows

The code will display the following output:

[1] TRUE

[1] 5

[1] 8

Now we know the data we received as output is in data frame form. We can access, analyze, and perform multiple operations associated with data frames. 

For example:

Fetch the information of the person who has the maximum salary

# Create a data frame of the csv file.

info <- read.csv("reading.csv")

# fetch the max salary from a data frame.

sal <- max(info$salary)

# Get the person detail having max salary.

fetch_maxsalary <- subset(info, salary == max(salary))

print(fetch_maxsalary)

On executing the above code, it will display the following output:

     id name salary start_date dept

5 NA Jane 843.25 2015-03-27 Finance

 

Writing into CSV File

We can create the CSV File using the write.csv() function from existing data frames. The new csv file will be created in the working directory.

# Create the data frame.

info <- read.csv("reading.csv")

retval <- subset(info, as.Date(start_date) > as.Date("2014-01-01"))

# Write filtered data into a new file.

write.csv(retval,"writing.csv")

info_data <- read.csv("writing.csv")

print(info_data) //Print the data 

 

A to Z Full Forms and Acronyms