0% found this document useful (0 votes)
83 views154 pages

WWW - Cuchd.in Campus: Gharuan, Mohali

The document provides an overview of basic R syntax and features including: - The R prompt uses ">" and calculations can be performed at the prompt. - Variables are assigned with "=" and different data types like numeric, integer, character, and logical can be created and manipulated. - Basic arithmetic operators like +, -, *, / can be used to perform calculations. - Functions are invoked with parentheses and arguments and comments start with "#". - Different objects like vectors and matrices can be created and their classes and types checked.

Uploaded by

Chandu Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views154 pages

WWW - Cuchd.in Campus: Gharuan, Mohali

The document provides an overview of basic R syntax and features including: - The R prompt uses ">" and calculations can be performed at the prompt. - Variables are assigned with "=" and different data types like numeric, integer, character, and logical can be created and manipulated. - Basic arithmetic operators like +, -, *, / can be used to perform calculations. - Functions are invoked with parentheses and arguments and comments start with "#". - Different objects like vectors and matrices can be created and their classes and types checked.

Uploaded by

Chandu Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 154

www.cuchd.

in Campus: Gharuan, Mohali


It is important to learn some basic syntax of the R programming
language before using to more sophisticated functions, graphics and
modeling. Below is a compilation of some of the basic features of R
that will get you going and help you to understand the R language.

R prompt:

The default R prompt is the greater-than sign (>)

After R started , there is a console waiting for input. At prompt (>),


You can enter numbers and perform calculation.

www.cuchd.in Campus: Gharuan, Mohali


Variable Name Validity Reason
var_name2. valid Has letters, numbers, dot and underscore
Has the character '%'. Only dot(.) and underscore
var_name% Invalid
allowed.

2var_name invalid Starts with a number

.var_name , Can start with a dot(.) but the dot(.)should not be


valid
var.name followed by a number.

The starting dot is followed by a number making it


.2var_name invalid
invalid

_var_name
www.cuchd.in invalid Starts with _ which is not valid
Campus: Gharuan, Mohali
Your First R Session

www.cuchd.in Campus: Gharuan, Mohali


We assign values to variables with the assignment
operator "=". We just typing the variable by itself at the prompt
‘>’ will print out the value. We should note that another form of
assignment operator "<-" is also in use.
>x=1
# When we type ‘x =1' and hit the <Enter> key, R will show
>x prompt ‘>’ and again we type x the result of the value x ,
[1] 1 which is 1 .
# When we type ‘x =45' and hit the <Enter> key, R will show
>x<-45 prompt, Again we type ‘y=36’ R will show prompt ‘>’ and hit
the <Enter> key, R will show prompt, we type z = x+y and hit
>y<-36 the <Enter> key, the prompt ‘>’ will shown again and type z
>z = x+y and hit the <Enter> key, the result of the value z , which is 81
>z .

[1] 81
www.cuchd.in Campus: Gharuan, Mohali
>2* # When we want to type ‘2*4’ but the line is incomplete. If a
+4 line is not syntactically completed, then a continuation prompts
[1] 8 (+) appears.

Functions:
# R functions are invoked by its name, then followed by the
> c(1, 2, 3) parenthesis, and zero or more arguments. The following apply
[1] 1 2 3 the function c to combine three numeric values into a vector.

Comments: # All text after the pound sign "#" within the same line is
considered a comment.
>1+1
# this is comment
[1] 2

www.cuchd.in Campus: Gharuan, Mohali


These objects can then be used in other calculations. To print
the object just enter the name of the object. There are some
restrictions when giving an object a name.
Object names cannot contain `strange' symbols like
!, +, -, #.
A dot (.) and an underscore ( _ ) are allowed, also a
name starting with a dot.
Object names can contain a number but cannot start
with a number.
R is case sensitive, X and x are two different
objects, as well as temp and temP.

www.cuchd.in Campus: Gharuan, Mohali


> 1+1
[1] 2 When we type '1+1' and hit the <Enter> key, R will
show the result of the calculation, which is equal to 2 .

> 15 -2 When we type '15 -2' and hit the <Enter> key, R will
[1] 13 show the result of the calculation, which is equal to
13 .
> 20*5
[1] 100 When we type ’20*5' and hit the <Enter> key, R will
show the result of the calculation, which is equal to
100 .
> 10/2 When we type '10/2' and hit the <Enter> key, R will
[1] 5 show the result of the calculation, which is equal to 5 .

www.cuchd.in Campus: Gharuan, Mohali


If you have forgotten to save your last expression, this can be
retrieved through an internal object .Last.value
>2*4
[1] 8
> value <- .Last.value
> value
[1] 8
Removing Objects
The functions rm() or remove() are used to remove objects
from the working directory
> rm(value)
> value
Error: Object ’value’ not found
www.cuchd.in Campus: Gharuan, Mohali
There are four basic (automatic) data type in R
languages:
Numeric

Integer

Character

Logical

Complex number

www.cuchd.in Campus: Gharuan, Mohali


Decimal values are called numeric's data type in R. It is the default
computational data type. If we assign a decimal value to a variable x as
follows, x will be of numeric type
> x = 120.5 # assign a decimal value
>x # print the value of x
[1] 120.5
> class(x) # print the class name of x
[1] "numeric"
>Values<-500 # assign a value with assignment operator
>values
[1]500
> a<- 5; b<- sqrt(2) # assign a value a & b with sqrt() function
> a; b
[1] 5 # print the value of a and square root of b
[1] 1.414214

www.cuchd.in Campus: Gharuan, Mohali


In order to create an integer variable in R, we invoke
the as.integer function. We can be assured that y is indeed
an integer by applying the is.integer function.
> a = as.integer(9)
>a # print the value of a
[1] 9
> class(y) # print the class name of y
[1] "integer"
> is.integer(y) # is y an integer?
[1] TRUE
Incidentally, we can coerce a numeric value into an integer with
the same as.integer function.

> as.integer(3.14) # coerce a numeric value


[1] 3
www.cuchd.in Campus: Gharuan, Mohali
we can parse a string for decimal values in much the same way
> as.integer(“25.7") # coerce a decimal string
[1] 25
On the other hand, it is erroneous trying to parse a non-decimal
string.
> as.integer("Johan") # coerce an non−decimal string
[1] NA #Warning message:
NAs introduced by coercion

Often, it is useful to perform arithmetic on logical values. Like the


C language, TRUE has the value 1, while FALSE has value 0.

> as.integer(TRUE) # the numeric value of TRUE


[1] 1
> as.integer(FALSE) # the numeric value of FALSE
[1] 0
www.cuchd.in Campus: Gharuan, Mohali
A character object is used to represent string values in
R. We convert objects into character values with
the as.character() function:
Character:
>string<- “Hello world” # to assign the string Hello world
>string
[1] “Hello world” # print the value of string
> a <- "1"; b <- 1 # to assign the string a as a “1” and b as a 1
> a; b
[1] "1" # print the value of string
[1] 1
> a <- "character"
> b <- "a"; c <- a
> a; b; c
[1] "Character"
[1] "a"
[1] "character"

www.cuchd.in Campus: Gharuan, Mohali


> x = as.character(3.14)
>x
# print the character string
[1] "3.14"
> class(x)
# print the class name of x
[1] "character"
Two character values can be concatenated with
the paste function.
> fname = “Denny"; lname =“Jo # to print the first & last
han" name as a character string
> paste(fname, lname)
[1] “Denny Johan"

www.cuchd.in Campus: Gharuan, Mohali


However, it is often more convenient to create a readable string with
the sprintf() function, which has a C language syntax.
> sprintf("%s has %d dollars", "Sam", 100)
[1] "Sam has 100 dollars"
To extract a substring, we apply the substr() function. Here is an example showing
how to extract the substring between the third and twelfth positions in a string

> s<-“Denny Johan is a tecrhnocat" # assign a string


> substr("denny johan is a technocrat",start=3, stop=14)
[1] "nny johan is" # print start with 3 no of position character and end
with 14 no of position character .
Note: counted character with blank space
To replace the first occurrence of the word “technocrat" by another word
“professional" in the string, we apply the sub() function
> sub("technocrat", "professional"," Johan is a technocrat")
[1] " Johan is a professional"

www.cuchd.in Campus: Gharuan, Mohali


A logical value is often created via comparison between variables.
> x = 10; y = 20 # sample values
>z=x>y # is x larger than y?
>z # print the logical value
[1] FALSE
> class(z) # print the class name of z
[1] "logical"
Standard logical operations are "&" (and), "|" (or), and "!" (negation).
> u = TRUE; v = FALSE
>u&v # u AND v
[1] FALSE
>u|v # u OR v
[1] TRUE
> !u # negation of u
[1] FALSE

www.cuchd.in Campus: Gharuan, Mohali


A complex value in R is defined via the pure imaginary
value i .
>Cn<-2+3i # create a complex number
>Cn # print the value of Cn
[1] 2+3i
> class(Cn) # print the class name of z
[1] "complex"
The following gives an error as −1 is not a complex value.

> sqrt(−1) # square root of −1


[1] NaN
Warning message:
In sqrt(−1) : NaNs produced

www.cuchd.in Campus: Gharuan, Mohali


Instead of, we have to use the complex value −1 + 0i.

> sqrt(−1+0i) # square root of −1+0i


[1] 0+1i

An alternative is to coerce −1 into a complex


value.

> sqrt(as.complex(−1))
[1] 0+1i

www.cuchd.in Campus: Gharuan, Mohali


The attributes of an object becomes important when
manipulating objects. All objects have two attributes, the
mode and their length.

Mode and Length:

The R function mode can be used to determine the mode of each


object, while the function length will help to determine each
object’s length.

mode: numeric, character, complex, logical

length: number of elements in object


www.cuchd.in Campus: Gharuan, Mohali
The R function mode can be used to determine the mode of
each object.

>value<-5 # assign value 5


> mode(value) # print mode
[1] “numeric” # mode is numeric
>string<-”My name is Johan” # assign string “ My name is
Johan”
> mode(string) #print mode
[1] "character” # mode is character

www.cuchd.in Campus: Gharuan, Mohali


>5<7 #assign 5 is less than 7
[1] TRUE # result is TRUE
> mode(5<7) #print mode.
[1] “logical” # mode is logical

>Cn<-2+3i #assign 2+3i


[1] 2+3i # show Cn is 2+3i
> mode(Cn) #print mode
[1] "complex“ #mode is complex

>sin(45) #assign sin(45)


[1] 0.8509035 #result of sin(45) is 0.8509035
> mode(sin) #print mode
[1] “Function” # mode is a function

>names(value) #the is no value in name function


[1] NULL #mode does not define the Null Value

www.cuchd.in Campus: Gharuan, Mohali


R function length will help to determine each object’s length.
> value<-5890 # assign value 5890
> length(value) # print Length
# Length is 1
[1] 1
# assign string my name is johan
> string<-"my name is johan" # print Length
> length(string) # Length is 1
[1] 1
# assign value 105 is greater than 458
> number<-105>458 # print Length
>length(number) # Length is 1
[1] 1

> sin<-75 # assign sin(75)


> length(sin) # print Length
# Length is 1
[1] 1

www.cuchd.in Campus: Gharuan, Mohali


In many practical examples, some of the data elements will not
be known and will therefore be assigned a missing value. The
code for missing values in R is NA. This indicates that the value
or element of the object is unknown. Any operation on an NA
results in an NA. The is.na() function can be used to check for
missing values in an object.
> value <- c(3,6,23,NA) # assign the values 3,6,23,NA
> is.na(value) # print the missing value
[1] FALSE FALSE FALSE TRUE # result TRUE show the missing value
> any(is.na(value))
[1] TRUE
> na.omit(value)
[1] 3 6 23
> attr(,"na.action")
[1] 4
> attr(,"class")
[1] "omit"
www.cuchd.in Campus: Gharuan, Mohali
Indefinite and Infinite values (Inf, -Inf and NaN) can also
be tested using the is.finite, is.infinite, is.nan and
is.number functions in a similar way as shown above.
These values come about usually from a division by zero
or taking the log of zero.
> value1<- 5/0
> value2<- log(0)
> value3<- 0/0
> cat("value1 = ",value1," value2 = ",value2, " value3 =
",value3,"\n")
value1 = Inf value2 = -Inf value3 = NaN
www.cuchd.in Campus: Gharuan, Mohali
This is basic arithmetic operators. The usual algebraic
rules of precedence apply (multiplication and division
take precedence over addition and subtraction). Use
parenthesis “( )” to separate operations of priority out. Do
not use bracket sets “[ ]” or “{}” as these are for other
purposes in R. Using arithmetic operators is very simple,
and operations can be performed directly on numbers or
on variables

www.cuchd.in Campus: Gharuan, Mohali


Arithmetic Operators Table

Operators Descriptions
+ Addition
- Subtraction
* Multiplication
/ Division
^ or ** Exponentiation
x %% y Modulus (x mod y) Exp. 5%%2 is 1
x %/% y Integer division Exp. 5%/%2 is 2

www.cuchd.in Campus: Gharuan, Mohali


Example of Arithmetic Operators
> 2+4
[1] 6
> y<-0
> x<-4
> x*y^2
[1] 0
> x^4
[1] 256
> z<-5
> (x+y)*z/x
[1] 5
www.cuchd.in Campus: Gharuan, Mohali
Logical & Relational Operators
Logical and relational operators are used when you want to
selectively execute code based on certain conditions.
Using logical and relational operators is a form of flow
control to determine the action the program will take.
Essentially flow control of a program can be thought of as
being in three layers – order (sequence of code written),
selection (use of logical and relational operators), and
repetition (or looping).

www.cuchd.in Campus: Gharuan, Mohali


Logical & Relational Operators

Operator Description
< less than
<= less than or equal to
> greater than
>= greater than or equal to
== exactly equal to
!= not equal to
! Not !x means not x
| OR x OR y
& AND x AND y
isTRUE(x) test if x is TRUE
www.cuchd.in Campus: Gharuan, Mohali
Logical & Relational Operators
Generally logical and relational operators are used with
conditional programming statements if and else.

The if statement can be used alone or with the else statement.


The form of the if statement is:
if (condition is true)
then do this
The condition in parenthesis usually will use a relational
operator to determine if the condition is true.

When the if statement is used alone, and the condition in


parenthesis is not true then nothing happen.

www.cuchd.in Campus: Gharuan, Mohali


Logical & Relational Operators

> x<-10 # if x is less than or equal to y then


add them to get z
>y<-6
> if(x<=y)z<- #condition is not true so nothing
x+y happens
>z
Error: Object #if x reverse relational operator is used then
"z" not found the condition is true and z is assigned x+y
> if(x>=y)z<-
x+y
>z
[1] 16
www.cuchd.in Campus: Gharuan, Mohali
Logical & Relational Operators
To get something to happen if the condition in
parenthesis is not true, use if in conjunction with else to
specify an alternative.

Note that there is not a condition in parenthesis after the


else statement.

In this case the code following else will execute as long


as the if condition is not true.
if (condition is true)
then do this
else
do this
www.cuchd.in Campus: Gharuan, Mohali
Logical & Relational
> q<-15 Operators
> t<-10
> if(q<t){w<-q+t} else w<-q-t # if else conditional statement written on
>w one line
[1] 5

Note that {} brackets around some of the code. These curly


bracket sets are frequently used to block sections of code, and
will indicate code continues on the next line. This code can also
be written:
> if(q<t){
+ w<-q+t # This separates the code onto different lines, which
is unnecessary for this simple case but with longer
+ }else code it becomes unwieldy to write all the code on
+ w<-q-t one line

www.cuchd.in Campus: Gharuan, Mohali


Logical & Relational Operators

The logical operators, &, |, and ! can be used to add


additional selection criteria to a selection statement.
if We want to simultaneously select based on two criteria you can
use the and (&) operator or the or (|) operator:
> a<-2
> b<-3
> c<-4 #Using and to test two
> if(a<b & b<c) x<-a+b+c conditions, both true
>x
[1] 9 #Using and to test two conditions,
> if(a>b & b<c) y<-a-b-c one is false
>y
Error: Object "y" not found #Using or to test two conditions,
> if(a==b | a>c) z<-a*b*c both false
>z
Error: Object "z" not found #Using or to test two conditions,
> if(a<b | a>c) z<-a*b*c one true
>z
[1] 24
Logical & Relational Operators
The not operator (!) is used for many purposes including
selecting based on inequality (“not equal to”)

> w<-2 # assign the value of w


> if(w!=3)v<-w # the condition w is not true

>v # print the value v

[1] 2
> if(w!=2)u<-w
>u
Error: Object "u" not found
Control Structures
Control by repetition, or looping, allows you to efficiently repeat
code without having to write the same code over and over.
Syntax Comments
if()...else Determine which set of expressions to run depending on whether or
not a condition is TRUE
ifelse() Do something to each element in a data structure depending on
whether or not a condition is TRUE for that particular element
switch() Evaluate different expressions depending on a given value
for() Loop for a fixed number of iterations
while() Loop until a condition is FALSE
repeat Repeat until iterations are halted with a call to break
break Break out of a loop
next Stop processing the current iteration and advance the looping index
if()...else Determine which set of expressions to run depending on whether or
not a condition is TRUE

See ?Control for the R documentation on control structures


If()…else
The condition needs to evaluate to a single logical value.

Brackets {} are not necessary if you only have one expression


and /or the if()...else statement are on one line.

To avoid a syntax error, you should NOT have a newline between


the closing bracket of the if statement and the else statement
if(condition) expression if TRUE
if(condition) {
expressions if TRUE
}
if(condition)expression if TRUE else expression if FALSE
if(condition) {
expressions if TRUE
} else {
expressions if FALSE
}
Example- If()…else

>x<- c(15,14,12,18,19,11)
> n <- length(x)
> sort.x <- sort(x)
> if(n%%2==0) {
+ median <-
(sort.x[n/2]+sort.x[1+n/2])/2
+ } else median <- sort.x[(n+1)/2]
> median
[1] 62
if else()
ifelse() returns a value with the same structure as test
which is filled with elements selected from either yes or no
depending on whether the element of test is TRUE or
FALSE. ifelse(test, yes, no)
> (x <- seq(3,15,len=6))
[1] 3.0 5.4 7.8 10.2 12.6 15.0

> (y <- matrix(1:8, nrow=2))


[,1] [,2] [,3] [,4]
[1,] 1 3 5 7
[2,] 2 4 6 8

> ifelse(y>3 & y <7, 1, 0)


[,1] [,2] [,3] [,4]
[1,] 0 0 1 0
[2,] 0 1 1 0
Switch()
The switch() function evaluates and returns different expressions
depending on the value of EXPR
switch(EXPR, ...)
EXPR needs to be a single character string or a number, not a vector

... is a comma separated list of name=expression or number=expression


If there is an exact match between EXPR and name/number then that
expression is evaluated and returned. If there is no expression, then the
next non-missing expression is used
If there is no match between EXPR and name/number, the value of the
first unnamed expression in ... is returned (unnamed expressions are like a
default).
The switch() function evaluates and returns different expressions
depending on the value of EXPR switch(EXPR, ...)
Example- Switch()

>central <- function(y, measure) { # Write a program


switch(measure,
Mean = ,
mean = mean(y),
median = median(y),
geometric = prod(y)^(1/length(y)),
"Invalid Measure")
}
> y <- runif(100) # Generate 100 random numbers
> central(y, "mean") # Print mean
[1] 0.5354678
> central(y, "Mean")
[1] 0.5354678
# Print median
> central(y, "median")
[1] 0.6016318
> central(y, "Median")
[1] "Invalid Measure"
> central(y, "geometric") # Print geometric mean
[1] 0.3783947
For Loop:
For loops are used to iterate through a process a specified
number of times.
for (var in seq) expr

A counter variable (usually designated by a lowercase letter


(“i”) is used to count how many times the loop is executed.
for (i in start:finish)
execute task
> y<-vector(mode="numeric") # we initialize a vector with values.
> for(i in 1:10){
+ y[i]<-i # we can loop through it to assign the values.
}
>y
[1] 1 2 3 4 5 6 7 8 9 10
While Loop:
The while loop repeats a condition while the expression in
parenthesis holds true and takes the form.

while (condition controlling flow is true)


perform task

> x<-0 # suppose we want to execute adding 1 to


> while(x<=5){x<-x+1} the variable x while x<=5
>x
[1] 6 # here x=6 at the end because the loop is still
true at the beginning of the loop
when x=5.
x <- 30L
if(is.integer(x)){
print("X is an Integer")
}

www.cuchd.in Campus: Gharuan, Mohali


x <- c("what","is","truth")
if("Truth" %in% x){
print("Truth is found")
} else {
print("Truth is not found")
}

www.cuchd.in Campus: Gharuan, Mohali


x <- c("what","is","truth")
if("Truth" %in% x){
print("Truth is found the first time")
} else if ("truth" %in% x) {
print("truth is found the second time")
} else {
print("No truth found")
}

www.cuchd.in Campus: Gharuan, Mohali


x <- switch(3,"first","second","third","fourth”)
print(x)

www.cuchd.in Campus: Gharuan, Mohali


v <- c("Hello","loop")
cnt <- 2
repeat{
print(v)
cnt <- cnt+1
if(cnt > 5){
break
}
www.cuchd.in Campus: Gharuan, Mohali
v <- c("Hello","while loop")
cnt <- 2
while (cnt < 7){
print(v)
cnt = cnt + 1
}
www.cuchd.in Campus: Gharuan, Mohali
v <- LETTERS[1:4]
for ( i in v) {
print(i)
}
www.cuchd.in Campus: Gharuan, Mohali
v <- LETTERS[1:6]
for ( i in v){
if (i == "D"){
next
}
print(i)
}
www.cuchd.in Campus: Gharuan, Mohali
# Create a sequence of numbers from 32 to 44.
print(seq(32,44))

# Find mean of numbers from 25 to 82.


print(mean(25:82))

# Find sum of numbers form 41 to 68.


print(sum(41:68))
www.cuchd.in Campus: Gharuan, Mohali
www.cuchd.in Campus: Gharuan, Mohali
Vectors :
Vectors are one-dimensional
arrays that can hold numeric
data, logical, integer, double,
complex, character and raw
data. The combine function c()
is used to form the vector.
www.cuchd.in Campus: Gharuan, Mohali
Vector Creation :Single Element Vector:
Scalars are one-element vectors.
Even when we write just one
value in R, it becomes a vector
of length 1 and belongs to one of
the above vector types.
www.cuchd.in Campus: Gharuan, Mohali
# Atomic vector of type character.
print("abc");

# Atomic vector of type double.


print(12.5)

# Atomic vector of type integer.


print(63L)
www.cuchd.in Campus: Gharuan, Mohali
# Atomic vector of type logical.
print(TRUE)
# Atomic vector of type complex.
print(2+3i)
# Atomic vector of type raw.
print(charToRaw('hello'))

www.cuchd.in Campus: Gharuan, Mohali


MULTIPLE VECTORS
# Creating a sequence from 5 to 13.
v <- 5:13
print(v)
# Creating a sequence from 6.6 to 12.6.
v <- 6.6:12.6
print(v)
# If the final element specified does not belong
to the sequence then it is discarded.
v <- 3.8:11.4
print(v)
www.cuchd.in Campus: Gharuan, Mohali
Using sequence (Seq.) operator

# Create vector with elements from 5 to 9


incrementing by 0.4.

print(seq(5, 9, by=0.4))
Seq(1:15)
Seq(2,50,by=4)

www.cuchd.in Campus: Gharuan, Mohali


Using the c() function

a <- c(1, 2, 5, 3, 6, -2, 4)


b <- c("one", "two", "three")
c <- c(TRUE, TRUE, TRUE, FALSE, TRUE, FALSE)

s <- c('apple','red',5,TRUE)
print(s)

www.cuchd.in Campus: Gharuan, Mohali


# Accessing vector elements using position.
t <-c("Sun","Mon","Tue","Wed","Thurs","Fri","Sat")
u <- t[c(2,3,6)]
print(u)
# Accessing vector elements using logical indexing.
v <-t[c(TRUE,FALSE,FALSE,FALSE,FALSE,TRUE,FALSE)]
print(v)
# Accessing vector elements using negative indexing.
x <- t[c(-2,-5)]
print(x)

www.cuchd.in Campus: Gharuan, Mohali


# Accessing vector elements
using 0/1 indexing.
y <- t[c(0,0,0,0,0,0,1)]
print(y)
Vector Manipulation
# Create two vectors.
v1 <- c(3,8,4,5,0,11)
v2 <- c(4,11,0,8,1,2)
#Addition of Two vectors
add. add.result <- v1+v2
print(add.result)<- v1+v2
print(add.result)
www.cuchd.in Campus: Gharuan, Mohali
# Vector substraction.
sub.result <- v1-v2
print(sub.result)
# Vector multiplication.
multi.result <- v1*v2
print(multi.result)
# Vector division.
divi.result <- v1/v2
print(divi.result)
www.cuchd.in Campus: Gharuan, Mohali
Vector Element Recycling
If we apply arithmetic operations to two vectors of unequal
length, then the elements of the shorter vector are recycled
to complete the operations
v1 <- c(3,8,4,5,0,11)
v2 <- c(4,11)
# V2 becomes c(4,11,4,11,4,11)
add.result <- v1+v2
print(add.result)
sub.result <- v1-v2
print(sub.result)
sub.result <- v1*v2
print(sub.result)
www.cuchd.in Campus: Gharuan, Mohali
Vector Element Sorting
v <- c(3,8,4,5,0,11, -9, 304)
# Sort the elements of the vector.
sort.result <- sort(v)
print(sort.result)
# Sort the elements in the reverse order.
revsort.result <- sort(v, decreasing = TRUE)
print(revsort.result)

www.cuchd.in Campus: Gharuan, Mohali


# Sorting character vectors.
v <- c("Red","Blue","yellow","violet")
sort.result <- sort(v)
print(sort.result)

# Sorting character vectors in reverse order.


revsort.result <- sort(v, decreasing = TRUE)
print(revsort.result)

www.cuchd.in Campus: Gharuan, Mohali


Lists :
Lists are the R objects which contain
elements of different types like -
numbers, strings, vectors and another
list inside it. A list can also contain a
matrix or a function as its elements.
List is created using list() function.

www.cuchd.in Campus: Gharuan, Mohali


Lists are the most complex of the R data types.
Basically, a list is an ordered collection of objects
(components). A list allows you to gather a variety of
(possibly unrelated) objects under one name. For
example, a list may contain a combination of vectors,
matrices, data frames, and even other lists. You create
a list using the list() function :
mylist <- list(object1, object2, …)
where the objects are any of the structures seen so far.
Optionally, you can name the objects in a list:
mylist <- list(name1=object1, name2=object2, …)
www.cuchd.in Campus: Gharuan, Mohali
Creating a List

To create a list containing strings, numbers,


vectors and a logical values.

#Create a list containing strings, numbers, vectors and a


logical values.

list_data <- list("Red", "Green", c(21,32,11), TRUE, 51.23,


119.1)
print(list_data)

www.cuchd.in Campus: Gharuan, Mohali


Naming List Elements

# Create a list containing a vector, a matrix and a list.


list_data <- list(c("Jan","Feb","Mar"),
matrix(c(3,9,5,1,-2,8), nrow=2),
list("green",12.3))
# Give names to the elements in the list.
names(list_data) <- c("1st Quarter", "A_Matrix", "A
Inner list")
# Show the list.
print(list_data)

www.cuchd.in Campus: Gharuan, Mohali


Manipulating List Elements:
We can add, delete and update list elements as shown
below. We can add and delete elements only at the end
of a list. But we can update any element
# Create a list containing a vector, a matrix and a list.
list_data <- list(c("Jan","Feb","Mar"),
matrix(c(3,9,5,1,-2,8), nrow=2),
list("green",12.3))
# Give names to the elements in the list.
names(list_data) <- c("1st Quarter", "A_Matrix", "A
Inner list")
www.cuchd.in Campus: Gharuan, Mohali
# Add element at the end of the list.
list_data[4] <- "New element"
print(list_data[4])
# Remove the last element.
list_data[4] <- NULL
# Print the 4th Element.
print(list_data[4])
# Update the 3rd Element.
list_data[3] <- "updated element"
print(list_data[3])
www.cuchd.in Campus: Gharuan, Mohali
Merging Lists:
# Create two lists.
list1 <- list(1,2,3)
list2 <- list("Sun","Mon","Tue")
# Merge the two lists.
merged.list <- c(list1,list2)
# Print the merged list.
print(merged.list)

www.cuchd.in Campus: Gharuan, Mohali


Converting List to Vector:
A list can be converted to a vector so
that the elements of the vector can be
used for further manipulation. All the
arithmetic operations on vectors can be
applied after the list is converted into
vectors. To do this conversion, we use
the unlist() function. It takes the list
as input and produces a vector.
www.cuchd.in Campus: Gharuan, Mohali
Create lists.
list1 <- list(1:5)
print(list1)
list2 <-list(10:14)
print(list2)
# Convert the lists to vectors.
v1 <- unlist(list1)
v2 <- unlist(list2)
print(v1)
print(v2)
# Now add the vectors
result <- v1+v2
print(result)
www.cuchd.in Campus: Gharuan, Mohali
Matrices :
Matrix are the R objects in which the elements
are arranged in a two-dimensional rectangular
layout. They contain elements of the same
atomic types. Though we can create a matrix
containing only characters or only logical
values, they are not of much use. We use
matrices containing numeric elements to be used
in mathematical calculations.
A Matrix is created using the matrix() function
www.cuchd.in Campus: Gharuan, Mohali
Syntax:
The basic syntax for creating a matrix in R is:
matrix(data, nrow, ncol, byrow, dimnames)
Following is the description of the parameters used:
data is the input vector which becomes the data
elements of the matrix.
nrow is the number of rows to be created.
ncol is the number of columns to be created.
byrow is a logical clue. If TRUE then the input vector
elements are arranged by row.
dimname is the names assigned to the rows and
columns.
www.cuchd.in Campus: Gharuan, Mohali
# Elements are arranged sequentially by row.
M <- matrix(c(3:14), nrow=4, byrow=TRUE)
print(M)
# Elements are arranged sequentially by column.
N <- matrix(c(3:14), nrow=4, byrow=FALSE)
print(N)
# Define the column and row names.
rownames = c("row1", "row2", "row3", "row4")
colnames = c("col1", "col2", "col3")
P <- matrix(c(3:14), nrow=4, byrow=TRUE,
dimnames=list(rownames, colnames))
print(P)
www.cuchd.in Campus: Gharuan, Mohali
Accessing Elements of a Matrix
Elements of a matrix can be accessed by using the column and
row index of the element. We consider the matrix P above to
find the specific elements below:
# Define the column and row names.
rownames = c("row1", "row2", "row3", "row4")
colnames = c("col1", "col2", "col3")
# Create the matrix.
P <- matrix(c(3:14), nrow=4, byrow=TRUE,
dimnames=list(rownames, colnames))
# Access the element at 3rd column and 1st row.
print(P[1,3])
.
www.cuchd.in Campus: Gharuan, Mohali
# Access the element at 2nd column and 4th
row.
print(P[4,2])
# Access only the 2nd row.
print(P[2,])
# Access only the 3rd column.
print(P[,3])

www.cuchd.in Campus: Gharuan, Mohali


> y <- matrix(1:20, nrow=5, ncol=4)
>y
[,1] [,2] [,3] [,4]
[1,] 1 6 11 16
[2,] 2 7 12 17
[3,] 3 8 13 18
[4,] 4 9 14 19
[5,] 5 10 15 20
www.cuchd.in Campus: Gharuan, Mohali
# 2x2 matrix filled by rows
> cells <- c(1,26,24,68)
> rnames <- c("R1", "R2")
> cnames <- c("C1", "C2")
> mymatrix <- matrix(cells, nrow=2, ncol=2,
byrow=TRUE,
dimnames=list(rnames, cnames))
>Mymatrix
C1 C2
R1 1 26
R2 24 68

www.cuchd.in Campus: Gharuan, Mohali


# 2x2 matrix filled by Colums
> mymatrix <- matrix(cells, nrow=2,
ncol=2, byrow=FALSE,
dimnames=list(rnames, cnames))
> mymatrix
C1 C2
R1 1 24
R2 26 68
www.cuchd.in Campus: Gharuan, Mohali
Matrix Computations:
Various mathematical operations are
performed on the matrices using the R
operators.
The result of the operation is also a
matrix.
The dimensions (number of rows and
columns) should be same for the
matrices involved in the operation.
www.cuchd.in Campus: Gharuan, Mohali
Matrix Addition & Subtraction

# Create two 2x3 matrices.


matrix1 <- matrix(c(3, 9, -1, 4, 2, 6), nrow=2)
print(matrix1)
matrix2 <- matrix(c(5, 2, 0, 9, 3, 4), nrow=2)
print(matrix2)
# Add the matrices.
result <- matrix1 + matrix2
cat("Result of addition","\n")
print(result)
www.cuchd.in Campus: Gharuan, Mohali
# Subtract the matrices
result <- matrix1 - matrix2
cat("Result of subtraction","\n")
print(result)

www.cuchd.in Campus: Gharuan, Mohali


Matrix Multiplication & Division

# Create two 2x3 matrices.


matrix1 <- matrix(c(3, 9, -1, 4, 2, 6), nrow=2)
print(matrix1)
matrix2 <- matrix(c(5, 2, 0, 9, 3, 4), nrow=2)
print(matrix2)

# Multiply the matrices.


result <- matrix1 * matrix2
cat("Result of multiplication","\n")
print(result)
www.cuchd.in Campus: Gharuan, Mohali
# Divide the matrices
result <- matrix1 / matrix2
cat("Result of division","\n")
print(result)

www.cuchd.in Campus: Gharuan, Mohali


Arrays :
Arrays are the R data objects which can
store data in more than two dimensions. For
example - If we create an array of
dimension (2, 3, 4) then it creates 4
rectangular matrices each with 2 rows and 3
columns. Arrays can store only data type.
An array is created using the array()
function. It takes vectors as input and uses
the values in the dim parameter to create an
array.
www.cuchd.in Campus: Gharuan, Mohali
Arrays are the R data objects which can store
data in more than two dimensions. For
example - If we create an array of dimension
(2, 3, 4) then it creates 4 rectangular matrices
each with 2 rows and 3 columns. Arrays can
store only data type.
An array is created using the array()
function. It takes vectors as input and uses
the values in the dim parameter to create an
array.
www.cuchd.in Campus: Gharuan, Mohali
Syntax
myarray <- array(vector, dimensions, dimnames)

# Create two vectors of different lengths.


vector1 <- c(5,9,3)
vector2 <- c(10,11,12,13,14,15)
# Take these vectors as input to the array.
result <-array(c(vector1,vector2),dim=c(3,3,2))
print(result)

www.cuchd.in Campus: Gharuan, Mohali


Accessing Array Elements
# Create two vectors of different lengths.

vector1 <- c(5,9,3)


vector2 <- c(10,11,12,13,14,15)
column.names <- c("COL1","COL2","COL3")
row.names <- c("ROW1","ROW2","ROW3")
matrix.names <- c("Matrix1","Matrix2")

www.cuchd.in Campus: Gharuan, Mohali


# Take these vectors as input to the array.

result <- array(c(vector1,vector2),dim=c(3,3,2),


dimnames=list(column.names,row.names,matrix.
names))

# Print the third row of the second matrix


of the array.
print(result[3,,2])
www.cuchd.in Campus: Gharuan, Mohali
# Print the element in the 1st row and 3rd
column of the 1st matrix.

print(result[1,3,1])

# Print the 2nd Matrix.

print(result[,,2])

www.cuchd.in Campus: Gharuan, Mohali


Manipulating Array Elements
# Create two vectors of different lengths.
vector1 <- c(5,9,3)
vector2 <- c(10,11,12,13,14,15)
# Take these vectors as input to the array.
array1 <-array(c(vector1,vector2),dim=c(3,3,2))
# Create two vectors of different lengths.
vector3 <- c(9,1,0)
vector4 <- c(6,0,11,3,14,1,2,6,9)
www.cuchd.in Campus: Gharuan, Mohali
array2 <- array(c(vector1,vector2),dim=c(3,3,2))
# create matrices from these arrays.
matrix1 <- array1[, , 2]
matrix2 <- array2[, ,2]
# Add the matrices.
result <- matrix1+matrix2
print(result)
www.cuchd.in Campus: Gharuan, Mohali
When we execute the above code, it produces the
following result:

[,1] [,2] [,3]


[1,] 10 20 26
[2,] 18 22 28
[3,] 6 24 30

www.cuchd.in Campus: Gharuan, Mohali


Factors :

Factors are the data objects which are used to


categorize the data and store it as levels.
They can store both strings and integers. They are
useful in the columns which have a limited
number of unique values. Like "Male, "Female"
and True, False etc. They are useful in data
analysis for statistical modelling.
Factors are created using the factor ()
function by taking a vector as input.

www.cuchd.in Campus: Gharuan, Mohali


variables can be described as nominal, ordinal,
or continuous. Nominal variables are
categorical, without an implied order. Diabetes
(Type1, Type2) is an example of a nominal
variable. Even if Type1 is coded as a 1 and
Type2 is coded as a 2 in the data, no order is
implied. Ordinal variables imply order but not
amount.
Status (poor, improved, excellent) is a good
example of an ordinal variable.
www.cuchd.in Campus: Gharuan, Mohali
Categorical (nominal) and ordered categorical (ordinal)
variables in R are called factors. Factors are crucial in R
because they determine how data will be analyzed and
presented visually.
The function factor() stores the categorical values as a
vector of integers in the
range [1... k] (where k is the number of unique values in the
nominal variable), and an internal vector of character
strings (the original values) mapped to these integers.
For example, assume that you have the vector
diabetes <- c("Type1", "Type2", "Type1", "Type1")

www.cuchd.in Campus: Gharuan, Mohali


Example:
patientID <- c(1, 2, 3, 4)
age <- c(25, 34, 28, 52)
diabetes <- c("Type1", "Type2", "Type1", "Type1")
status <- c("Poor", "Improved", "Excellent", "Poor")
diabetes <- factor(diabetes)
status <- factor(status, order=TRUE)
patientdata <- data.frame(patientID, age, diabetes,
status)
str(patientdata)
summary(patientdata)
www.cuchd.in Campus: Gharuan, Mohali
# Create a vector as input.
data <-
c("East","West","East","North","North"
,"East","West","West","West","East","
North")
print(data)
print(is.factor(data))
www.cuchd.in Campus: Gharuan, Mohali
# Apply the factor function.
factor_data <- factor(data)
print(factor_data)
print(is.factor(factor_data))
# Create the vectors for data frame.
height <- c(132,151,162,139,166,147,122)
weight <- c(48,49,66,53,67,52,40)
gender <-
c("male","male","female","female","male","female","male")

www.cuchd.in Campus: Gharuan, Mohali


# Create the data frame.
input_data <- data.frame(height,weight,gender)
print(input_data)
# Test if the gender column is a factor.
print(is.factor(input_data$gender))
# Print the gender column so see the
levels.
print(input_data$gender)
Generating Factor Levels:
We can generate factor levels by using the gl() function.
It takes two integers as input which indicates how
many levels and how many times each level.
Syntax
gl(n, k, labels)
Following is the description of the parameters used:
n is a integer giving the number of levels.
k is a integer giving the number of replications.
labels is a vector of labels for the resulting factor
levels

www.cuchd.in Campus: Gharuan, Mohali


v <- gl(3, 4, labels = c("Tampa", "Seattle","Boston"))
print(v)

www.cuchd.in Campus: Gharuan, Mohali


DATA FRAME:
A data frame is a table or a two-dimensional array-like
structure in which each column contains values of one
variable and each row contains one set of values from
each column.
Following are the characteristics of a data frame.
The column names should be non-empty.
The row names should be unique.
The data stored in a data frame can be of numeric,
factor or character type.
Each column should contain same number of data
items.
www.cuchd.in Campus: Gharuan, Mohali
A data frame is more general than a matrix in that
different columns can contain different modes of
data (numeric, character, etc.).
A data frame is created with the data.frame()
function :
mydata <- data.frame(col1, col2, col3,…)
where col1, col2, col3, … are column vectors of
any type (such as character, numeric,
or logical). Names for each column can be
provided with the names function.
www.cuchd.in Campus: Gharuan, Mohali
Creating a data frame:
patientID <- c(1, 2, 3, 4)
age <- c(25, 34, 28, 52)
diabetes <- c("Type1", "Type2", "Type1", "Type1")
status <- c("Poor", "Improved", "Excellent", "Poor")
patientdata <- data.frame(patientID, age, diabetes, status)
patientdata
patientID age diabetes status
1 1 25 Type1 Poor
2 2 34 Type2 Improved
3 3 28 Type1 Excellent
4 4 52 Type1 Poor
www.cuchd.in Campus: Gharuan, Mohali
# Create the data frame.
emp.data <- data.frame(
emp_id = c (1:5),
emp_name = c("Rick","Dan","Michelle","Ryan","Gary"),
Salary = c(623.3,515.2,611.0,729.0,843.25),
start_date = as.Date(c("2012-01-01","2013-09-23","2014-
11-15","2014-05-11","2015-03-27")),
stringsAsFactors=FALSE)
# Print the data frame.
print(emp.data)
# Get the structure of the data frame.
str(emp.data)
www.cuchd.in Campus: Gharuan, Mohali
# Print the summary.
print(summary(emp.data))
# Create the data frame.
emp.data <- data.frame(
emp_id = c (1:5), emp_name =
c("Rick","Dan","Michelle","Ryan","Gary"),
salary = c(623.3,515.2,611.0,729.0,843.25),
start_date = as.Date(c("2012-01-01","2013-09-23","2014-11-
15","2014-05-11","2015-03-27")),
stringsAsFactors=FALSE)
# Extract Specific columns.
result <- data.frame(emp.data$emp_name,emp.data$salary)
print(result)

www.cuchd.in Campus: Gharuan, Mohali


Extract Data from Data Frame
# Create the data frame.
emp.data <- data.frame(emp_id = c (1:5),
emp_name =c("Ram","Dinker","Mahesh","Riya",“Giya"),
salary = c(623.3,515.2,611.0,729.0,843.25),
start_date = as.Date(c("2016-01-01","2016-09-23","2016-
11-15","2016-05-11","2016-03-27")),
stringsAsFactors=FALSE)
# Extract Specific columns.
result <-data.frame(emp.data$emp_name,emp.data$salary)
print(result)

www.cuchd.in Campus: Gharuan, Mohali


# Extract first two rows.
result <- emp.data[1:2,]
print(result)
# Extract 3rd and 5th row with 2nd and 4th
column.
result <- emp.data[c(3,5),c(2,4)]
print(result)

www.cuchd.in Campus: Gharuan, Mohali


# Add the "dept" coulmn.
emp.data$dept <- c("IT","Operations","IT","HR","Finance")
v <- emp.data
print(v)

www.cuchd.in Campus: Gharuan, Mohali


# Create the first data frame
emp.data <- data.frame(emp_id = c (1:5),
emp_name=c("Ram","Dinker","Mahesh","Ri
ya",“Giya"),salary =
c(623.3,515.2,611.0,729.0,843.25),
start_date = as.Date(c("2016-01-01","2016-
09-23","2016-11-15","2016-05-11","2016-
03-27")), stringsAsFactors=FALSE)
Print(emp.data)

www.cuchd.in Campus: Gharuan, Mohali


# Create the second data frame
emp.newdata <- data.frame(
emp_id = c (6:8),emp_name =
c("Rasmi","Pranab","Tusar"),
salary = c(578.0,722.5,632.8),
start_date = as.Date(c("2013-05-21","2013-07-
30","2014-06-17")),
dept = c("IT","Operations","Fianance"),
stringsAsFactors=FALSE)
# Bind the two data frames.
emp.finaldata <- rbind(emp.data,emp.newdata)
print(emp.finaldata)
www.cuchd.in Campus: Gharuan, Mohali
# Create the data frame.
emp.data <- data.frame(
emp_id = c (1:5),
emp_name = c("Rick","Dan","Michelle","Ryan","Gary"),
salary = c(623.3,515.2,611.0,729.0,843.25),
start_date = as.Date(c("2012-01-01","2013-09-23","2014-11-
15","2014-05-11","2015-03-27")),stringsAsFactors=FALSE)
# Add the "dept" coulmn.
emp.data$dept <- c("IT","Operations","IT","HR","Finance")
v <- emp.data
print(v)

www.cuchd.in Campus: Gharuan, Mohali


Joining Columns and Rows in a Data Frame
We can join multiple vectors to create a data frame using the cbind()function.
Also we can merge two data frames using rbind() function.
# Create vector objects.
city <- c("Tampa","Seattle","Hartford","Denver")
state <- c("FL","WA","CT","CO")
zipcode <- c(33602,98104,06161,80294)
# Combine above three vectors into one data frame.
addresses <- cbind(city,state,zipcode)
# Print a header.
cat("# # # # The First data frame\n")
# Print the data frame.
print(addresses)
www.cuchd.in Campus: Gharuan, Mohali
# Create another data frame with similar columns
new.address <- data.frame(
city = c("Lowry","Charlotte"),
state = c("CO","FL"),
zipcode = c("80230","33949"),
stringsAsFactors=FALSE)
# Print a header.
cat("# # # The Second data frame\n")
# Print the data frame.
print(new.address)
www.cuchd.in Campus: Gharuan, Mohali
# Combine rows form both the data frames.
all.addresses <- rbind(addresses,new.address)
# Print a header.
cat("# # # The combined data frame\n")
# Print the result.
print(all.addresses)

www.cuchd.in Campus: Gharuan, Mohali


Merging Data Frames:
library(MASS)
merged.Pima <- merge(x=Pima.te, y=Pima.tr,
by.x=c("bp", "bmi"),
by.y=c("bp", "bmi"))
print(merged.Pima)
nrow(merged.Pima)

www.cuchd.in Campus: Gharuan, Mohali


Melting and Casting:
One of the most interesting aspects of R
programming is about changing the shape of the
data in multiple steps to get a desired shape. The
functions used to do this are called melt() and
cast().
library(MASS)
print(ships)

www.cuchd.in Campus: Gharuan, Mohali


Melt the Data:
Now, we melt the data to organize it,
converting all columns other than type and year
into multiple rows.

molten.ships <- melt(ships, id =


c("type","year"))
print(molten.ships)

www.cuchd.in Campus: Gharuan, Mohali


Cast the Molten Data
We can cast the molten data into a new form
where the aggregate of each type of ship for each
year is created. It is done using the cast()
function.
recasted.ship <- cast(molten.ships,
type+year~variable,sum)
print(recasted.ship)

www.cuchd.in Campus: Gharuan, Mohali


Transpose:
Use the t() function to transpose a matrix or a
data frame. In the later case, row names
become variable (column) names.

www.cuchd.in Campus: Gharuan, Mohali


PIE CHART
Syntax:
The basic syntax for creating a pie-chart using the R is:
pie(x, labels, radius, main, col, clockwise)
Following is the description of 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.(value between -1 and +1).
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 anticlockwise.
www.cuchd.in Campus: Gharuan, Mohali
# 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()
www.cuchd.in Campus: Gharuan, Mohali
# 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_title_colours.jpg")
# Plot the chart with title and rainbow color pallet.
pie(x, labels, main="City pie chart",
col=rainbow(length(x)))
# Save the file.
dev.off()
www.cuchd.in Campus: Gharuan, Mohali
# Create data for the graph.
x <- c(21, 62, 10,53)
labels <- c("London","New York","Singapore","Mumbai")
piepercent<- round(100*x/sum(x), 1)
# Give the chart file a name.
png(file = "city_percentage_legends.jpg")
# Plot the chart.
pie(x, labels=piepercent, main="City pie
chart",col=rainbow(length(x)))
legend("topright", c("London","New
York","Singapore","Mumbai"), cex=0.8,
fill=rainbow(length(x)))
# Save the file.
dev.off()
www.cuchd.in Campus: Gharuan, Mohali
# Get the library.
library(plotrix)
# Create data for the graph.
x <- c(21, 62, 10,53)
lbl <- c("London","New York","Singapore","Mumbai")
# Give the chart file a name.
png(file = "3d_pie_chart.jpg")
# Plot the chart.
pie3D(x,labels=lbl,explode=0.1,
main="Pie Chart of Countries ")
# Save the file.
dev.off()

www.cuchd.in Campus: Gharuan, Mohali


Graphical parameters
You can customize many features of a graph (fonts, colors, axes,
titles) through options called graphical parameters.
One way is to specify these options through the par() function.
Values set in this
manner will be in effect for the rest of the session or until they’re
changed. The
format is par(optionname=value, optionname=value, ...).
Specifying par()
without parameters produces a list of the current graphical settings.
Adding the no.readonly=TRUE option produces a list of current
graphical settings that can be
modified.

www.cuchd.in Campus: Gharuan, Mohali


dose <- c(20, 30, 40, 45, 60)
drugA <- c(16, 20, 27, 40, 60)
drugB <- c(15, 18, 25, 31, 40)
plot(dose, drugA, type="b")

opar <- par(no.readonly=TRUE)


par(lty=2, pch=17)
plot(dose, drugA, type="b")
par(opar)

www.cuchd.in Campus: Gharuan, Mohali


A bar chart represents data in rectangular bars with length of the bar proportional
to the value of the variable. R uses the function barplot() to create bar charts. R
can draw both vertical and horizontal bars in the bar chart. In bar chart each of
the bars can be given different colors.
Syntax
barplot(H,xlab,ylab,main, names.arg,col)
Following is the description of the parameters used:
H is a vector or matrix containing numeric values used in bar
chart.
xlab is the label for x axis.
ylab is the label for y axis.
main is the title of the bar chart.
names.arg is a vector of names appearing under each bar.
col is used to give colors to the bars in the graph.
www.cuchd.in Campus: Gharuan, Mohali
# Create the data for the chart.
H <- c(7,12,28,3,41)
# Give the chart file a name.
png(file = "barchart.png")
# Plot the bar chart.
barplot(H)
# Save the file.
dev.off()

www.cuchd.in Campus: Gharuan, Mohali


# Create the data for the chart.
H <- c(7,12,28,3,41)
M <- c("Mar","Apr","May","Jun","Jul")
# Give the chart file a name.
png(file = "barchart_months_revenue.png")
# Plot the bar chart.
barplot(H,names.arg=M,xlab="Month",ylab="Reve
nue",col="blue", main="Revenue chart",
border="red")
# Save the file.
dev.off()
www.cuchd.in Campus: Gharuan, Mohali
Parameter Description
Pch : Specifies the symbol to use when plotting
points (see figure 3.4).
cex : Specifies the symbol size. cex is a number
indicating the amount by which plotting
symbols should be scaled relative to the
default. 1=default, 1.5 is 50% larger, 0.5 is
50% smaller, and so forth.
Lty: Specifies the line type (see figure 3.5).
Lwd: Specifies the line width. lwd is expressed
relative to the default (default=1).
For example, lwd=2 generates a line twice
as wide as the default.
www.cuchd.in Campus: Gharuan, Mohali
plot(dose, drugA, type="b", lty=3, lwd=3, pch=15, cex=2)

www.cuchd.in Campus: Gharuan, Mohali


Parameter Description
col Default plotting color. Some functions (such as
lines and pie) accept a vector of values that are
recycled. For example, if col=c(“red”, “blue”)
and three lines are plotted, the first line will be
red, the second blue, and the third red.
col.axis Color for axis text.
col.lab Color for axis labels.
col.main Color for titles.
col.sub Color for subtitles.
fg The plot’s foreground color.
bg The plot’s background color.
www.cuchd.in Campus: Gharuan, Mohali
n <- 10
mycolors <- rainbow(n)
pie(rep(1, n), labels=mycolors, col=mycolors)
mygrays <- gray(0:n/n)
pie(rep(1, n), labels=mygrays, col=mygrays)

www.cuchd.in Campus: Gharuan, Mohali


# Create the input vectors.
colors <- c("green","orange","brown")
months <- c("Mar","Apr","May","Jun","Jul")
regions <- c("East","West","North")

www.cuchd.in Campus: Gharuan, Mohali


A bar chart represents data in rectangular
bars with length of the bar proportional to
the value of the variable. R uses the
function barplot() to create bar charts. R
can draw both vertical and horizontal bars
in the bar chart. In bar chart each of the
bars can be given different colors.

www.cuchd.in Campus: Gharuan, Mohali


Syntax
The basic syntax to create a bar-chart in R is:
barplot(H,xlab,ylab,main, names.arg,col)
Following is the description of the parameters used:
H is a vector or matrix containing numeric values
used in bar chart.
xlab is the label for x axis.
ylab is the label for y axis.
main is the title of the bar chart.
names.arg is a vector of names appearing under
each bar.
col is used to give colors to the bars in the graph.
www.cuchd.in Campus: Gharuan, Mohali
# Create the matrix of the values.
Values <-
matrix(c(2,9,3,11,9,4,8,7,3,12,5,2,8,10,11),nrow=3,ncol=5,byrow=T
RUE)
# Give the chart file a name.
png(file = "barchart_stacked.png")
# Create the bar chart.
barplot(Values,main="total
revenue",names.arg=months,xlab="month",ylab="revenue",col=col
ors)
# Add the legend to the chart.
legend("topleft", regions, cex=1.3, fill=colors)
# Save the file.
dev.off()
www.cuchd.in Campus: Gharuan, Mohali
# Create the data for the chart.
H <- c(7,12,28,3,41)
M <- c("Mar","Apr","May","Jun","Jul")
# Give the chart file a name.
png(file = "barchart_months_revenue.png")
# Plot the bar chart.
barplot(H,names.arg=M,xlab="Month",ylab="Re
venue",col="blue",
main="Revenue chart",border="red")
# Save the file.
dev.off()
www.cuchd.in Campus: Gharuan, Mohali
# Create the input vectors.
colors <- c("green","orange","brown")
months <- c("Mar","Apr","May","Jun","Jul")
regions <- c("East","West","North")
# Create the matrix of the values.
Values <- matrix(c(2,9,3,11,9,4,8,7,3,12,5,2,8,10,11),nrow=3,ncol=5,byrow=TRUE)
# Give the chart file a name.
png(file = "barchart_stacked.png")
# Create the bar chart.
barplot(Values,main="total
revenue",names.arg=months,xlab="month",ylab="revenue",col=colors)
# Add the legend to the chart.
legend("topleft", regions, cex=1.3, fill=colors)
# Save the file.
dev.off()

www.cuchd.in Campus: Gharuan, Mohali


Boxplots are a measure of how well distributed
is the data in a data set. It divides the data set
into three quartiles. This graph represents the
minimum, maximum, median, first quartile and
third quartile in the data set. It is also useful in
comparing the distribution of data across data
sets by drawing boxplots for each of them.
Boxplots are created in R by using the
boxplot() function
www.cuchd.in Campus: Gharuan, Mohali
Syntax
The basic syntax to create a boxplot in R is :
boxplot(x, data, notch,varwidth, names,main)
Following is the description of the parameters used:
x is a vector or a formula.
data is the data frame.
notch is a logical value. Set as TRUE to draw a notch.
varwidth is a logical value. Set as true to draw width of the
box proportionate to the sample size.
names are the group labels which will be printed under each
box plot.
main is used to give a title to the graph

www.cuchd.in Campus: Gharuan, Mohali


# Give the chart file a name.
png(file = "boxplot.png")
# Plot the chart.
boxplot(mpg ~ cyl, data=mtcars,
xlab="Number of Cylinders",
ylab="Miles Per Gallon",
main="Mileage Data")
# Save the file.
dev.off()

www.cuchd.in Campus: Gharuan, Mohali


# Give the chart file a name.
png(file = "boxplot_with_notch.png")
# Plot the chart.
boxplot(mpg ~ cyl, data=mtcars,
xlab="Number of Cylinders",ylab="Miles Per
Gallon",
main="MileageData“,notch=TRUE,varwidth=TRUE,
col=c("green","yellow","purple"),names=c("High",
"Medium","Low"))
# Save the file.
dev.off()
www.cuchd.in Campus: Gharuan, Mohali
A histogram represents the frequencies of values
of a variable bucketed into ranges.
Histogram is similar to bar chat but the difference
is it groups the values into continuous ranges.
Each bar in histogram represents the height of the
number of values present in that range.
R creates histogram using hist() function. This
function takes a vector as an input and uses
some more parameters to plot histograms

www.cuchd.in Campus: Gharuan, Mohali


Syntax
The basic syntax for creating a histogram using R is:
hist(v,main,xlab,xlim,ylim,breaks,col,border)
Following is the description of the parameters used:
v is a vector containing numeric values used in
histogram.
main indicates title of the chart.
col is used to set color of the bars.
border is used to set border color of each bar.
xlab is used to give description of x-axis.
xlim is used to specify the range of values on the x-axis.
ylim is used to specify the range of values on the y-axis.
breaks is used to mention the width of each bar.
www.cuchd.in Campus: Gharuan, Mohali
# Create data for the graph.
v <- c(9,13,21,8,36,22,12,41,31,33,19)
# Give the chart file a name.
png(file = "histogram.png")
# Create the histogram.
hist(v,xlab="Weight",col="yellow",border="blue")
# Save the file.
dev.off()

www.cuchd.in Campus: Gharuan, Mohali


# Create data for the graph.
v <- c(9,13,21,8,36,22,12,41,31,33,19)
# Give the chart file a name.
png(file = "histogram_lim_breaks.png")
# Create the histogram.
hist(v,xlab="Weight",col="green",border="red",xli
m = c(0,40), ylim = c(0,5),
breaks = 5 )
# Save the file.
dev.off()
www.cuchd.in Campus: Gharuan, Mohali

You might also like