R PROGRAMMING GUIDE
4. Reading and Writing Data
In this chapter, we will be focusing on reading and writing data which means
we will be focusing import and export feature. Importing a data is more on
reading excel or text file and manipulating them using various statements.
This chapter is divided into two basic parts namely “Import” and “Export”
feature of R programming language.
Exporting Data:
Consider for the following example where we create a simple dataset. Now, as
a developer our main focus is to export it in various formats. Now we will
create a dataset with parameters such as Age, Name etc.
> x <- [Link]("SN" = 1:2, "Age" = c(21,15), "Name" = c("John","Dora"))
>x
SN Age Name
1 1 21 John
2 2 15 Dora
> [Link](x, file="[Link]")
With [Link]() we can convert the dataset in CSV format. The output is
visible in Rstudio as mentioned below:
Page 19 Eduonix Learning Solutions Pvt Ltd
R PROGRAMMING GUIDE
Writing tab delimited files
We can create tab delimited file or basically text file of the same data frame as
mentioned below:
Syntax:
[Link](mydata, file = "[Link]")
Example:
> [Link](x, file = "[Link]")
The output of file creation is mentioned in following snapshot:
Writing Excel file
We need a package called “xlsx” for writing the dataset in excel format. The
complete procedure to do is mentioned below:
> [Link]("xlsx”)
> library(xlsx)
> [Link](x,file = "[Link]")
Page 20 Eduonix Learning Solutions Pvt Ltd
R PROGRAMMING GUIDE
The output is displayed below:
Page 21 Eduonix Learning Solutions Pvt Ltd
R PROGRAMMING GUIDE
Writing Stata files:
The following demonstration is used to create a stata file from the given CSV
file.
> library("foreign")
> [Link](x,
+ datafile="[Link]",
+ codefile="[Link]",
+ package="Stata")
The output is created in the following manner:
Importing Data Files
Now we will focus on the reverse way of importing a particular data file in
R programming language. We will consider various type of file imports in
this section
Page 22 Eduonix Learning Solutions Pvt Ltd
R PROGRAMMING GUIDE
1. CSV file
In the previous section, we created a CSV file named “[Link]”. Now we will
import the same file in this section. This can be achieved with a function called
[Link]()
> mydata <- [Link]("[Link]")
> mydata
X1 X21 John
1 2 15 Dora
2. Reading a Tab-delimited Text File
We can achieve import function from text file using function “[Link]”. The
implementation of function is mentioned below:
> mydata <-[Link]("[Link]")
> mydata
[Link]
1 1 1 21 John
2 2 2 15 Dora
3. Excel file
R programming language includes an easy manipulation to import the dataset
and execute them in systematic manner with excel sheet representation as
mentioned below:
library(readxl)
mydata <- read_excel("[Link]")
View(mydata)
> mydata
# A tibble: 2 x 4
X__1 SN Age Name
<chr> <dbl> <dbl> <chr>
11 1 21 John
22 2 15 Dora
Page 23 Eduonix Learning Solutions Pvt Ltd
R PROGRAMMING GUIDE
4. Stata Files:
R programming also includes a basic feature to write and import the stata files.
Suppose your R data frame has the dataset fuzzybunny and as a user wants to
save the file to the C: drive within the directory of documents as
[Link].
After loading the foreign library,
library(foreign)
Here’s the syntax:
[Link](fuzzybunny, “C://Documents/[Link]”)
Summary
Datasets are supplied with R or packages related to them can be made
available with the data function.
A user can import data into R with a very wide range of external sources.
There are lots of external packages for reading excel files, including xlsx.
The foreign package reads a particular data from the associated software.
Page 24 Eduonix Learning Solutions Pvt Ltd