Data Handling in R Programming - GeeksforGeeks

10 May, 2020
R Programming Language is used for statistics and data analytics purposes. Importing and exporting of data is often used in all these applications of R programming.R language has the ability to read different types of files such as comma-separated values (CSV) files, text files, excel sheets and files, SPSS files, SAS files, etc.
R allows its users to work smoothly with the systems directories with the help of some pre-defined functions that take the path of the directory as the argument or return the path of the current directory that the user is working on. Below are some directory functions in R:
getwd(): This function is used to get the current working directory being used by R.
setwd(): This function in R is used to change the path of current working directory and the path of the directory is passed as argument in the function.Example:setwd("C:/RExamples/")
ORsetwd("C:\\RExamples\\")

setwd("C:/RExamples/")

OR
setwd("C:\\RExamples\\")

list.files(): This function lists all files and folders present in current working directory.
Let us take some basic files to import in R to learn the approach of importing data files:
In R language, text files can be read using read.table() function.
Syntax:
read.table(filename, header = FALSE, sep = "")

Parameters:header represents if the file contains header row or notsep represents the delimiter value used in file
To know about all the arguments of read.table(), execute below command in R:
help("read.table")
Example:Suppose a file is present in the current working directory and using R programming, import the data from that particular text file and the content of text file is as shown:
100 A a
200 B b
300 C c
400 D d
500 E e
600 F f

Values are separated by white spaces.
# Check current working directorygetwd()  # Get content into a data framedata <- read.table("TextFileExample.txt",                     header = FALSE, sep = " ")  # Printing content of Text Fileprint(data)  # Print the class of dataprint(class(data))
Output:
[1] "C:/Users/xyz/Documents"
   V1 V2 V3
1 100  A  a
2 200  B  b
3 300  C  c
4 400  D  d
5 500  E  e
6 600  F  f
[1] "data.frame"

Comma separated values or CSV files can be imported and read in R using read.csv() function.
Syntax:
read.csv(filename, header = FALSE, sep = "")

Parameters:header represents if the file contains header row or notsep represents the delimiter value used in file
To know about all the arguments of read.csv(), execute below command in R:
help("read.csv")
Example:Suppose a csv file is present in the current working directory and the content of file is as shown:
# Check current working directorygetwd()  # Get content into a data framedata <- read.csv("CSVFileExample.csv",                   header = FALSE,sep = "\t")  # Printing content of Text Fileprint(data)  # Print the class of dataprint(class(data))
Output:
[1] "C:/Users/xyz/Documents"
   V1 V2 V3
1 100 AB ab
2 200 CD cd
3 300 EF ef
4 400 GH gh
5 500 IJ ij
[1] "data.frame"

To read and import the excel files, “xlsx” package is required to use the read.xlsx() function. To read “.xls” excel files, “gdata” package is required to use read.xls() function.
Syntax:
read.xlsx(filename, sheetIndex)

OR
read.xlsx(filename, sheetName)

Parameters:sheetIndex specifies number of sheetsheetName specifies name of sheet
To know about all the arguments of read.xlsx(), execute below command in R:
help("read.xlsx")
Example:Suppose a xlsx file is present in the current working directory and the content of file is as shown:
# Install xlsx packageinstall.packages("xlsx")  library(xlsx)  # Check current working directorygetwd()  # Get content into a data framedata <- read.xlsx("ExcelExample.xlsx",                    sheetIndex = 1,                    header = FALSE)  # Printing content of Text Fileprint(data)  # Print the class of dataprint(class(data))
Output:
[1] "C:/Users/xyz/Documents"
    X1  X2  X3
1 1000 ABC abc
2 2000 DEF def
3 3000 GHI ghi
4 4000 JKL jkl
5 5000 MNO mno
[1] "data.frame"

Below are some methods to export the data to a file in R:
Using consolecat() function in R is used to output the object to console. It can be also used as redirecting the output to a particular file.Syntax:cat(..., file)
Parameter:file specifies the filename to which output has to redirectedTo know about all the arguments of cat(), execute below command in R:help("cat")Example:str = "World"  # Redirect Output to file cat("Hello, ", str, file = "catExample.txt")Output:Above code creates a new file and redirects the output of cat(). The contents of the file are shown below after executing the code-Hello,  World

Syntax:
cat(..., file)

Parameter:file specifies the filename to which output has to redirected
To know about all the arguments of cat(), execute below command in R:
help("cat")
Example:
str = "World"  # Redirect Output to file cat("Hello, ", str, file = "catExample.txt")
Output:Above code creates a new file and redirects the output of cat(). The contents of the file are shown below after executing the code-
Hello,  World

Using sink() function:sink() function is used to redirect all the outputs from cat() and print() to the given filename.Syntax:sink(filename) # begins redirecting output to file
.
.
sink()
To know about all the arguments of sink(), execute below command in R:help("sink")Example:# Begin redirecting outputsink("SinkExample.txt")  x <- c(1, 3, 4, 5, 10)print(mean(x))print(class(x))print(median(x))  sink()Output:The above code creates a new file and redirects the output. The contents of the file are shown below after executing the code-[1] 4.6
[1] "numeric"
[1] 4

sink(filename) # begins redirecting output to file
.
.
sink()

To know about all the arguments of sink(), execute below command in R:
help("sink")
Example:
# Begin redirecting outputsink("SinkExample.txt")  x <- c(1, 3, 4, 5, 10)print(mean(x))print(class(x))print(median(x))  sink()
Output:The above code creates a new file and redirects the output. The contents of the file are shown below after executing the code-
[1] 4.6
[1] "numeric"
[1] 4

A matrix or data-frame object can be redirected and written to csv file using write.csv() function.
Syntax: write.csv(x, file)
Parameter:file specifies the file name used for writing
To know about all the arguments of write.csv(), execute below command in R:
help("write.csv")
Example:
# Create vectorsx <- c(1, 3, 4, 5, 10)y <- c(2, 4, 6, 8, 10)z <- c(10, 12, 14, 16, 18)  # Create matrixdata <- cbind(x, y, z)  # Writing matrix to CSV Filewrite.csv(data, file = "CSVWrite.csv", row.names = FALSE)
Output:Above code creates a new file and redirects the output. The contents of the file is shown below after executing the code-
R-FileHandling
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Change column name of a given DataFrame in R
How to Replace specific values in column in R DataFrame ?
Adding elements in a vector in R programming - append() method
How to change Row Names of DataFrame in R ?
Filter data by multiple conditions in R using Dplyr
Loops in R (for, while, repeat)
Change Color of Bars in Barchart using ggplot2 in R
Convert Factor to Numeric and Numeric to Factor in R Programming
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr