In R language, we can read data from files stored outside the R locale. We can also write data to files that will be stored and accessed by the operating system. R language can read and write to various file formats, such ascsv
,excel
,xml
etc.
In this chapter, we will learn fromcsv
File reads data and then writes data tocsv
File. The file should exist in the current working directory so that it can be read by R. Of course we can also set up our own directory and read the files from there.
Get and set up working directory
You can usegetwd()
Function checks the directory pointed to by the R language workspace. You can also usesetwd()
Function sets a new working directory.
# Get and print current working directory. print(getwd()) # Set current working directory. setwd("/web/com") # Get and print current working directory. print(getwd())
When we execute the above code, it produces the following result
[1] "/web/com/1441086124_2016" [1] "/web/com"
This result depends on your operating system and the directory you are currently working on.
Enter as a CSV file
A csv file is a text file where the values in the column are separated by commas. Let's consider the nameThe following data appears in the file.
You can create this file using Windows Notepad by copying and pasting this data. Use the Save as All Files in Notepad(*.*)
Options Save the file as。
id,name,salary,start_date,dept 1,Rick,623.3,2012-01-01,IT 2,Dan,515.2,2013-09-23,Operations 3,Michelle,611,2014-11-15,IT 4,Ryan,729,2014-05-11,HR ,Gary,843.25,2015-03-27,Finance 6,Nina,578,2013-05-21,IT 7,Simon,632.8,2013-07-30,Operations 8,Guru,722.5,2014-06-17,Finance
Read CSV file
The following is()
A simple example of a function to read CSV files available in the current working directory
data <- ("") print(data)
When we execute the above code, it produces the following result
id, name, salary, start_date, dept 1 1 Rick 623.30 2012-01-01 IT 2 2 Dan 515.20 2013-09-23 Operations 3 3 Michelle 611.00 2014-11-15 IT 4 4 Ryan 729.00 2014-05-11 HR 5 NA Gary 843.25 2015-03-27 Finance 6 6 Nina 578.00 2013-05-21 IT 7 7 Simon 632.80 2013-07-30 Operations 8 8 Guru 722.50 2014-06-17 Finance
Analyze CSV files
By default,()
The function takes the output as a data frame. This can be easily checked as follows. Additionally, we can check the number of columns and rows.
data <- ("") print((data)) print(ncol(data)) print(nrow(data))
When we execute the above code, it produces the following result
[1] TRUE [1] 5 [1] 8
Once we read the data in the data frame, we can apply all functions that apply to the data frame as described in the following section.
Get the maximum salary
# Create a data frame. data <- ("") # Get the max salary from data frame. sal <- max(data$salary) print(sal)
When we execute the above code, it produces the following result
[1] 843.25
Get detailed information about people with maximum salary
We can get rows that meet specific filter conditions, similar toSQL where
clause.
# Create a data frame. data <- ("") # Get the max salary from data frame. sal <- max(data$salary) # Get the person detail having max salary. retval <- subset(data, salary == max(salary)) print(retval)
When we execute the above code, it produces the following result
id name salary start_date dept 5 NA Gary 843.25 2015-03-27 Finance
Get information about all IT department employees
# Create a data frame. data <- ("") retval <- subset( data, dept == "IT") print(retval)
When we execute the above code, it produces the following result
id name salary start_date dept 1 1 Rick 623.3 2012-01-01 IT 3 3 Michelle 611.0 2014-11-15 IT 6 6 Nina 578.0 2013-05-21 IT
Personnel in IT departments who receive salary of more than 600
# Create a data frame. data <- ("") info <- subset(data, salary > 600 & dept == "IT") print(info)
When we execute the above code, it produces the following result
id name salary start_date dept 1 1 Rick 623.3 2012-01-01 IT 3 3 Michelle 611.0 2014-11-15 IT
Those who have been awarded 2014 or later
# Create a data frame. data <- ("") retval <- subset(data, (start_date) > ("2014-01-01")) print(retval)
When we execute the above code, it produces the following result
id name salary start_date dept 3 3 Michelle 611.00 2014-11-15 IT 4 4 Ryan 729.00 2014-05-11 HR 5 NA Gary 843.25 2015-03-27 Finance 8 8 Guru 722.50 2014-06-17 Finance
Write to CSV file
R language can be createdcsv
Existing data frames in file form.()
Functions are used to createcsv
File. This file is created in the working directory.
# Create a data frame. data <- ("") retval <- subset(data, (start_date) > ("2014-01-01")) # Write filtered data into a new file. (retval,"") newdata <- ("") print(newdata)
When we execute the above code, it produces the following result
X id name salary start_date dept 1 3 3 Michelle 611.00 2014-11-15 IT 2 4 4 Ryan 729.00 2014-05-11 HR 3 5 NA Gary 843.25 2015-03-27 Finance 4 8 8 Guru 722.50 2014-06-17 Finance
Here column X comes from the datasetnewper
. This can be deleted using additional parameters when writing to the file.
# Create a data frame. data <- ("") retval <- subset(data, (start_date) > ("2014-01-01")) # Write filtered data into a new file. (retval,"", = FALSE) newdata <- ("") print(newdata)
When we execute the above code, it produces the following result
id name salary start_date dept 1 3 Michelle 611.00 2014-11-15 IT 2 4 Ryan 729.00 2014-05-11 HR 3 NA Gary 843.25 2015-03-27 Finance 4 8 Guru 722.50 2014-06-17 Finance
This is the article about R language’s explanation of CSV file operation examples. For more related contents of R language CSV file operation, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!