Introduction To R
Introduction To R
What R covered…
• Basic introduction of R
• Features of R
• Programming features of R
• Exploring RGUI and RStudio
• Basic concepts in R
• Working with R environment
• Handling data in R workspace.
• Reading datasets and exporting data from R
BIG DATA WITH 5 V’S
BIG DATA AND HADOOP
BigData processing with tools and techniques of
Hadoop.
Hadoop is an open source, Java-based programming
framework.
Hadoop supports the processing and storage of
extremely large data sets.
The processing uses a framework called as MapReduce.
A storage with distributed file system called HDFS.
• R is a well-developed.
• R is a simple and effective programming language
includes conditionals, loops, functions & input &
output facilities.
Statisticians
at John Deere use R for time series
modelling and geospatial analysis in a reliable and
reproducible way.
• Contributed packages:
-User-defined packages.
-Packages written by various users.
GUI-Graphical User Interface
• R language has several editors.
• RGUI
• Rstudio
• Deducer
• RKWard
• Rweka
• Eclipse StartET
• Emacs Speaks Statistics
• jEdit
Common GUI for R
• RGUI
• Specifies a tool with pre-compiled version of R for
Microsoft Windows.
• Rstudio
• Specifies cross-platform and open source IDE for R
programming development.
Exploring RGUI
• RGUI consists of
-R Console
-Development of program
-Quitting R
Exploring RStudio
• RStudio is a code editor and development
environment.
• Predefined functions in R:
>sum(10,12,13)
>rep(“Hello”, 5)
>sqrt(100)
Working with Vectors
• Vector can be defined as a single entity consisting of
an ordered collection of numbers.
• Object creation in R:
object.name= mathematical.expression
>myobj=20
>myobj
20
>myobj=25+12/2-16
>myobj
5
Interacting with users
• We can write R script to interact with users.
• The readline() function helps to ask questions from
user.
>msg=“Welcome ”
>yourname=readline(“What is your name?”)
What is your name?CSE
>paste(msg, yourname)
“Welcome CSE”
Handling data in R workspace
• R working environment includes variables, functions,
vectors, matrices, data frames and lists.
• Functions in R:
-The ls() function
-The rm() function
-The getwd() function
-The save() function
-The load() function
The ls() function
• The ls() function is used to view/list all created
variables in current active R workspace.
>ls()
>msg=“Hello”
>myobj=25+12/2-16
>hw=c(“Hello”, “World”)
>ls()
“hw” “msg” “myobj”
The rm() function
• The rm() function is used to remove the variables
that are not required anymore in a session.
>rm()
>msg=“Hello”
>myobj=25+12/2-16
>hw=c(“Hello”, “World”)
>ls()
“hw” “msg” “myobj”
>rm(hw)
>ls()
“msg” “myobj”
The getwd() function
• getwd()- Get working directory
>getwd()
“D:/Myfiles”
The save() function
• save()-save created variables.
>msg=“Hello”
>save(msg, file=“name.rda”)
print(“Hello World”)
p<-1:5
P
q<-20
r<-p+q
r
ls()
Reading & writing data from R
• Analysis can not perform without data.
>combine=c(data, mydata)
>combine
Using scan() command
• The c() command uses commas to separate values.
• The scan() command is used to separate values
without commas.
• Syntax:
>scan() #Ask to enter data from users.
Reading numerical values using scan() command
>scan()
12
13
14
15
16
Read 5 items
12 13 14 15 16
• Syntax:
>scan(what = ‘character’)
• The syntax:
>read.csv()
>read.table(file.choose(), header=TRUE,
row.names=1)
>var=read.table(file.choose(), header=TRUE,
row.names=1, sep=“\t”)
>var
Exporting data from R
• Writing data To move processed data out of R.
Export data from R to other source.
>my=scan(what=‘character’)
>write.csv(my, “E:/file.txt”, sep=“,”)
PIE CHART IN R
In R the pie chart is created using the pie() function.
It takes positive numbers as a vector input.
pie(x, labels, radius, main, col, clockwise)
The parameters used −
x is a vector containing the numeric values used in the
pie chart.
labels is used to give description to the slices.
radius indicates the radius of the circle of the pie chart.
main indicates the title of the chart.
col indicates the color palette.
clockwise is a logical value indicating if the slices are
drawn clockwise or anti clockwise.
PIE CHART EXAMPLE
# Create data for the graph.
x <- c(21, 62, 10, 53)
labels <- c("London", "New York",
"Singapore", "Mumbai")
# Give the chart file a name.
png(file = "city.jpg")
# Plot the chart.
pie(x,labels)
# Save the file.
dev.off()
BAR CHART IN R
R uses the function barplot() to create bar charts.
R draw both vertical and Horizontal bars in bar chart.
barplot(H,xlab,ylab,main, names.arg,col)