Professional Documents
Culture Documents
Week 7
Week 7
a) Reading different types of data sets (.txt, .csv) from Web or disk and
writing in file in specific disk location.
Aim: To read different types of data sets (.txt, .csv) from Web or disk and writing in file in
specific disk location.
Description:
Reads a file in table format and creates a data frame from it, with cases corresponding to lines
and variables to fields in the file.
Usage:
read.table(file, header = FALSE, sep = "", quote = "\"'",dec = ".", row.names, col.names,
na.strings = "NA", colClasses = NA, nrows = -1, encoding = "unknown", text)
read.csv(file, header = TRUE, sep = ",", quote = "\"", dec = ".", ...)
Arguments:
Write.table:
1
write.table prints its required argument x (after converting it to a data frame if it is not one
nor a matrix) to a file or connection.
Usage:
write.table(x, file = "", append = FALSE, quote = TRUE, sep = " ", eol = "\n", na = "NA",
dec = ".", row.names = TRUE, col.names = TRUE, fileEncoding = "")
write.csv(...)
write.csv2(...)
Arguments:
2
Source code:
> rt<-read.table("c:/Users/Dell/Documents/sample.txt",header=TRUE)
> print(rt)
> write.table(rt,"c:/Users/Dell/Documents/s2.txt",quote=FALSE)
> head(my_data)
> rt2<-read.table("c:/Users/Dell/Documents/s2.txt",header=TRUE,sep=',')
> print(rt2)
> head(rt)
Output:
1 IND1 10 A
2 IND2 7 A
3 IND3 20 A
4 IND4 14 A
5 IND5 14 A
6 IND6 12 A
name.age.gender.company.salary
3
4 4 nandu 20 f tcs 20k
4
b) Reading Excel data sheet in R.
Description:
read_excel() function is basically used to import/read an excel file and it can only be
accessed after importing of the readxl library in R language..
Usage:
read_xlsx( path, sheet = NULL, range = NULL, col_names = TRUE, col_types = NULL, na
= "", trim_ws = TRUE, n_max = Inf, guess_max = min(1000, n_max),.name_repair =
"unique")
Arguments:
5
Source code:
Output:
PRODUCT PRICE X
1 NA NA
2 Refriegerator 1200 NA
3 oven 750 NA
4 Dishwasher 600 NA
5 Cofeemaker 300 NA
6
c) Reading XML data set into R.
Description:
XML is a file format which shares both the file format and the data on the World Wide Web,
intranets, and elsewhere using standard ASCII text. It stands for Extensible Markup
Language (XML). Similar to HTML it contains markup tags. But unlike HTML where the
markup tag describes structure of the page, in xml the markup tags describe the meaning of
the data contained into he file.
You can read a xml file in R using the "XML" package. This package can be installed using
following command.
install.packages("XML")
Input Data
Create a XMl file by copying the below data into a text editor like notepad. Save the file with
a .xml extension and choosing the file type as all files(*.*).
Sourcecode:
>install.packages("XML")
> library("XML")
> library("methods")
> print(d)
> library("XML")
Warning message:
> library("methods")
7
> # Give the input file name to the function.
> print(result)
Output:
<?xml version="1.0"?>
<RECORDS>
<EMPLOYEE>
<ID>1</ID>
<NAME>Rick</NAME>
<SALARY>623.3</SALARY>
<STARTDATE>1/1/2012</STARTDATE>
<DEPT>IT</DEPT>
</EMPLOYEE>
<EMPLOYEE>
<ID>2</ID>
<NAME>Dan</NAME>
<SALARY>515.2</SALARY>
<STARTDATE>9/23/2013</STARTDATE>
<DEPT>Operations</DEPT>
</EMPLOYEE>
<EMPLOYEE>
<ID>3</ID>
<NAME>Michelle</NAME>
<SALARY>611</SALARY>
<STARTDATE>11/15/2014</STARTDATE>
<DEPT>IT</DEPT>
</EMPLOYEE>
<EMPLOYEE>
<ID>4</ID>
<NAME>Ryan</NAME>
<SALARY>729</SALARY>
<STARTDATE>5/11/2014</STARTDATE>
<DEPT>HR</DEPT>
8
</EMPLOYEE>
<EMPLOYEE>
<ID>5</ID>
<NAME>Gary</NAME>
<SALARY>843.25</SALARY>
<STARTDATE>3/27/2015</STARTDATE>
<DEPT>Finance</DEPT>
</EMPLOYEE>
<EMPLOYEE>
<ID>6</ID>
<NAME>Nina</NAME>
<SALARY>578</SALARY>
<STARTDATE>5/21/2013</STARTDATE>
<DEPT>IT</DEPT>
</EMPLOYEE>
<EMPLOYEE>
<ID>7</ID>
<NAME>Simon</NAME>
<SALARY>632.8</SALARY>
<STARTDATE>7/30/2013</STARTDATE>
<DEPT>Operations</DEPT>
</EMPLOYEE>
<EMPLOYEE>
<ID>8</ID>
<NAME>Guru</NAME>
<SALARY>722.5</SALARY>
<STARTDATE>6/17/2014</STARTDATE>
<DEPT>Finance</DEPT>
</EMPLOYEE>
</RECORDS>
Output:
9
3 3 Michelle 611 11/15/2014 IT
10