Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Data Handling in R Programming

  • Last Updated : 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/")
    

    OR

    setwd("C:\\RExamples\\")
    
  • list.files(): This function lists all files and folders present in current working directory.

Importing Files in R

Let us take some basic files to import in R to learn the approach of importing data files:



Importing Text 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 not
sep 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 directory
getwd()
  
# Get content into a data frame
data <- read.table("TextFileExample.txt"
                    header = FALSE, sep = " ")
  
# Printing content of Text File
print(data)
  
# Print the class of data
print(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"

Importing CSV Files

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 not
sep 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 directory
getwd()
  
# Get content into a data frame
data <- read.csv("CSVFileExample.csv"
                  header = FALSE,sep = "\t")
  
# Printing content of Text File
print(data)
  
# Print the class of data
print(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"

Importing Excel File

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 sheet
sheetName 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 package
install.packages("xlsx")
  
library(xlsx)
  
# Check current working directory
getwd()
  
# Get content into a data frame
data <- read.xlsx("ExcelExample.xlsx"
                   sheetIndex = 1
                   header = FALSE)
  
# Printing content of Text File
print(data)
  
# Print the class of data
print(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"

Exporting files in R

Below are some methods to export the data to a file in R:

  • Using console
    cat() 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 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 output
    sink("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
    

Writing to CSV files

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 vectors
x <- c(1, 3, 4, 5, 10)
y <- c(2, 4, 6, 8, 10)
z <- c(10, 12, 14, 16, 18)
  
# Create matrix
data <- cbind(x, y, z)
  
# Writing matrix to CSV File
write.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-



My Personal Notes arrow_drop_up
Recommended Articles
Page :