R Notes-Unit-3
R Notes-Unit-3
R Notes-Unit-3
What is R
Why Use R?
1. Statistical Features of R:
Basic Statistics: The most common basic statistics terms are the
mean, mode, and median. These are all known as “Measures of
Central Tendency.” So using the R language we can measure central
tendency very easily.
Static graphics: R is rich with facilities for creating and developing
interesting static graphics. R contains functionality for many plot types
including graphic maps, mosaic plots, biplots, and the list goes on.
Probability distributions: Probability distributions play a vital role in
statistics and by using R we can easily handle various types of
probability distribution such as Binomial Distribution, Normal
Distribution, Chi-squared Distribution and many more.
2. Programming Features of R:
Disadvantages of R:
Formal Definition of R:
Programming in R:
Since R is much similar to other widely used languages syntactically, it
is easier to code and learn in R.
Programs can be written in R in any of the widely used IDE like R
Studio, Rattle, Tinn-R, etc.
( for online editor datacamp.com)
After writing the program save the file with the extension .r.
To run the program use the following command on the command line:
R file_name.r
Example:
cat("Welcome to R Programming!")
Output:
Welcome to R Programming!
Printing in R:
1. Unlike many other programming languages, you can output code in R without
using a print function. To output text in R, use single or double quotes:
Ex: “Hello World”
2. However, when your code is inside an R expression (for example inside
curly braces {} ), use the print() function to output the result.
Ex: for (x in 1:10) {
print(x)
}
3. print("HelloWorld")
Output:”Hello World”
4. print("HelloWorld", quote=FALSE)
Output: Hello World
5. myString<-“Hello World!”
print(myString)
Output: Hello World!
Comments in R:
R Variables:
Variable Names: A variable can have a short name (like x and y) or a more
descriptive name (age, carname, total_volume).
Rules for R variables are:
Creating Variables in R
R does not have a command for declaring a variable.
A variable is created the moment you first assign a value to it.
To assign a value to a variable, use any of these =, ->, <- signs.
To output (or print) the variable value, just type the variable name:
Ex: name <- "abc"
age <- 20
Output:
The class of var_x is character
Now the class of var_x is numeric
Next the class of var_x becomes integer
Syntax
variable_name = value #using equal to operator
print(var1)
print(var2)
print(var3)
Output:
[1] "hello"
[1] "hello"
[1] "hello"
Multiple Variables
R allows you to assign the same value to multiple variables in one line:
Output:
[1] "Orange"
[1] "Orange"
[1] "Orange"
Print / Output Variables
However, R does have a print() function available if you want to use it.
Concatenate Elements
Output: R is awesome
paste(text1, text2)
Output: R is awesome
For numbers, the + character works as a mathematical operator:
num1 + num2
Output: 15
Variable Assignment:
print(var.1)
Note − The vector c(TRUE,1) has a mix of logical and numeric class. So logical
class is coerced to numeric class making TRUE as 1.
Important Methods for Variables:
These methods are used to determine the data type of the variable,
finding a variable, deleting a variable, etc.
Syntax:class(variable)
print(class(var1))
Output:
[1] "character"
2. ls() function:
This builtin function is used to know all the present variables in the
workspace. This is generally helpful when dealing with a large number of
variables at once and helps prevents overwriting any of them.
Syntax: ls()
print(ls())
Output:
[1] "var1" "var2" "var3"
3. rm() function:
This is again a built-in function used to delete an unwanted variable within
your workspace. This helps clear the memory space allocated to certain
variables that are not in use thereby creating more space for others. The
name of the variable to be deleted is passed as an argument to it.
Syntax: rm(variable)
print(var3)
Output:
Error in print(var3) : object 'var3' not found
Execution halted
R-Data Types
Each variable in R has an associated data type.
Each data type requires different amounts of memory and has some
specific operations which can be performed over it.
R has the following basic data types and the following table shows
the data type and the values that each data type can take.
Basic Data Values Example
Types
Numeric Set of all real numbers 10.5, 55, 787
Example:
# numeric
x <- 10.5
class(x)
# integer
x <- 1000L
class(x)
# complex
x <- 9i + 3
class(x)
# character/string
x <- "R is exciting"
class(x)
# logical/boolean
x <- TRUE
class(x)
Output:
[1] "numeric"
[1] "integer"
[1] "complex"
[1] "character"
[1] "logical"
Example:
x=4
y=3
z=x>y
print(z)
print(class(z))
Output:
[1] TRUE
[1] "logical"
Example:
Output:
4c 65 61 72 6e 69 6e 67 20 72 20 70 72 6f 67 72 61 6d 6d 69 6e 67
To find the data type of an object you have to use class() function.
The syntax for doing that is you need to pass the object as an
argument to the function class() to find the data type of an object.
Syntax: class(object)
Example
print(class(TRUE))
print(class(3L))
print(class(10.5))
print(class(1+2i))
print(class("12-04-2020"))
Output:
[1] "logical"
[1] "integer"
[1] "numeric"
[1] "complex"
[1] "character"
Type verification:
Example
print(is.logical(TRUE))
print(is.integer(3L))
print(is.numeric(10.5))
print(is.complex(1+2i))
print(is.character("12-04-2020"))
print(is.integer("a"))
print(is.numeric(2+3i))
Output:
[1] TRUE
[1] TRUE
[1] TRUE
[1] TRUE
[1] TRUE
[1] FALSE
[1] FALSE
Using this we can change or convert the data type of one object to
another.
To perform this action we have to use the prefix “as.” before the
data type as the command.
The syntax for doing that is as.data_type() of the object which we
want to coerce.
Syntax: as.data_type(object)
Example
print(as.numeric(TRUE))
print(as.complex(3L))
print(as.logical(10.5))
print(as.character(1+2i))
print(as.numeric("12-04-2020"))
Output:
[1] 1
[1] 3+0i
[1] TRUE
[1] "1+2i"
[1] NA
Warning message:
In print(as.numeric("12-04-2020")) : NAs introduced by coercion
Note: All the coercions are not possible and if attempted will be
returning an “NA” value.
Operators
Types of Operators:
Arithmetic Operators
Relational Operators
Logical Operators
Assignment Operators
Miscellaneous Operators
Basic Operations
>a
[1] 11.00 3.00 28.00 1.75
[1] 4
[1] 4
[1] 3
[1] 43.75
[1] 1 3 6 10 15 21 28 36 45 55
[1] 10.9375
[1] 7
[1]NULL
> var(a) # calculate the variance
[1] 146.1823
[1] 12.09059
25%
2.6875
> summary(a)
Arithmetic Operators
Output:
Example:
cat ("Vector1 less than Vector2 :", vec1 < vec2, "\n")
cat ("Vector1 less than equal to Vector2 :", vec1 <= vec2, "\n")
cat ("Vector1 greater than Vector2 :", vec1 > vec2, "\n")
cat ("Vector1 greater than equal to Vector2 :", vec1 >= vec2, "\n")
cat ("Vector1 not equal to Vector2 :", vec1 != vec2, "\n")
cat ("Vector1 equal to Vector2 :", vec1 == vec2, "\n")
Output:
Output : FALSE
Output : TRUE
Example:
Output:
Example:
Output:
vector 1 : 2 3 4 5
vector 2 : 2 3 4 5
vector 3 : 2 3 4 5
vector 4 : 2 3 4 5
vector 5 : 2 3 4 5
Miscellaneous Operators
These operators are used to for specific purpose and not general
mathematical or logical computation.
These are the mixed operators that simulate the printing of sequences
and assignment of vectors, either left or right handedly.
Miscellaneous operators are used to manipulate data
Operator Description
: Colon operator. It creates the series of numbers in sequence for
a vector.
%in% This operator is used to identify if an element belongs to a vector.
%*% This operator is used to multiply a matrix with its transpose.
Example:
v <- 2:8
print(v)
Output:
[1] 2 3 4 5 6 7 8
Example:
v1 <- 8
v2 <- 12
t <- 1:10
print(v1 %in% t)
print(v2 %in% t)
Output:
[1] TRUE
[1] FALSE
Example:
Output:
[,1] [,2]
[1,] 65 82
[2,] 82 117
Conditional Statements
1. if condition
2. if-else condition
3. for loop
4. nested loops
5. while loop
6. repeat and break statement
7. return statement
8. next statement
if condition
Syntax:
if(boolean_expression) {
Flow Diagram:
Example:
a <- 33
b <- 200
if (b > a) {
print("b is greater than a")
}
Output:
Example:
x <- 100
Output:
Example:
x <- 30L
if(is.integer(x)) {
print("X is an Integer")
}
Output:
Syntax:
if(boolean_expression) {
} else {
Example:
a <- 33
b <- 33
if (b > a) {
print("b is greater than a")
} else if (a == b) {
print ("a and b are equal")
}
Output:
Example:
x <- 5
Output:
Example:
x <- c("what","is","truth")
if("Truth" %in% x) {
print("Truth is found")
} else {
print("Truth is not found")
}
Output:
Syntax:
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")
}
Output:
Example:
x <- 41
if (x > 10) {
print("Above ten")
if (x > 20) {
print("and also above 20!")
} else {
print("but not above 20")
}
} else {
print("below 10")
}
Output:
Syntax:
x <- switch(3,"first","second","third","fourth")
print(x)
Output:
[1] "third"
Example:
a= 1
b= 2
y = switch(a+b, "C", "C++", "JAVA", "R" )
print (y)
Output:
Example:
x= "2"
y="1"
a = switch( paste(x,y,sep=""),
"9"="Hello C",
"12"="Hello C++",
"18"="Hello JAVA",
"21"="Hello R"
)
print (a)
Output:
val1 = 6
val2 = 7
val3 = "a"
result = switch(val3,
"a"= cat("Addition =", val1 + val2),
"s"= cat("Subtraction =", val1 - val2),
"d"= cat("Division = ", val1 / val2),
"m"= cat("Multiplication =", val1 * val2),
"u"= cat("Modulus =", val1 %% val2),
"p"= cat("Power =", val1 ^ val2))
print(result)
Output:
Addition = 13NULL
Example:
y = "18"
a=10
b=2
x = switch(y,
"9"=cat("Addition=",a+b),
"12"=cat("Subtraction =",a-b),
"18"=cat("Division= ",a/b),
"21"=cat("multiplication =",a*b)
)
print (x)
Output:
Division= 5NULL
Looping
for – Loop
Syntax:
Output:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10
Example:
Output:
[1] "Apple"
[1] "Orange"
[1] "Guava"
[1] "Pinapple"
[1] "Banana"
[1] "Grapes"
Example:
print(i)
}
Output:
[1] 1
[1] 3
[1] 5
Example: Creating an empty list and using for statement populate the list
Output:
[1] 1 4 9 16 25
Example:
x <- c(2,5,3,9,8,11,6,44,43,47,67,95,33,65,12,45,12)
count <- 0
for (val in x) {
if(val %% 2 == 0) count = count+1
}
print(count)
Output:
[1] 6
Example:
for (i in 1:3)
{
for (j in 1:i)
{
print(i * j)
}
}
Output:
[1] 1
[1] 2
[1] 4
[1] 3
[1] 6
[1] 9
Example:
for (i in 1:2)
{
for (j in 1:3)
{
print(i * j)
}
}
Output:
[1] 1
[1] 2
[1] 3
[1] 2
[1] 4
[1] 6
Example:
adj <- list("red", "big", "tasty")
fruits <- list("apple", "banana", "cherry")
for (x in adj) {
for (y in fruits) {
print(paste(x, y))
}
}
Output:
Jump Statements
Example:
for (i in c(3, 6, 23, 19, 0, 21))
{
if (i == 0)
{
break
}
print(i)
}
print("Outside Loop")
Output:
[1] 3
[1] 6
[1] 23
[1] 19
Example:
fruits <- list("apple", "banana", "cherry")
for (x in fruits) {
if (x == "cherry") {
break
}
print(x)
}
Output:
[1] "apple"
[1] "banana"
Example:
fruits <- list("apple", "banana", "cherry")
for (x in fruits) {
if (x == "banana") {
next
}
print(x)
}
Output:
[1] "apple"
[1] "cherry"
Example:
for (i in c(3, 6, 23, 19, 0, 21))
{
if (i == 0)
{
next
}
print(i)
}
print('Outside Loop')
Output:
[1] 3
[1] 6
[1] 23
[1] 19
[1] 21
Example:
v <- LETTERS[1:6]
for ( i in v) {
if (i == "D") {
next
}
print(i)
}
Output:
[1] "A"
[1] "B"
[1] "C"
[1] "E"
[1] "F"
Example:
j<-0
while(j<10){
if (j==7){
j=j+1
next
}
cat("\nnumber is =",j)
j=j+1
}
Output:
number is = 0
number is = 1
number is = 2
number is = 3
number is = 4
number is = 5
number is = 6
number is = 8
number is = 9
While-loop
statement
}
Example:
i <- 1
while (i < 6) {
print(i)
i <- i + 1
}
Output:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
Example:
v <- c("Hello","while loop","example")
cnt <- 2
while (cnt < 7) {
print(v)
cnt = cnt + 1
}
Output:
[1] "Hello" "while loop" "example"
[1] "Hello" "while loop" "example"
[1] "Hello" "while loop" "example"
[1] "Hello" "while loop" "example"
[1] "Hello" "while loop" "example"
Repeat-loop
Syntax:
repeat {
commands
if(condition) {
break
}
}
Example:
v <- c("Hello","loop")
cnt <- 2
repeat {
print(v)
cnt <- cnt+1
if(cnt > 5) {
break
}
}
Output:
[1] "Hello" "loop"
[1] "Hello" "loop"
[1] "Hello" "loop"
[1] "Hello" "loop"
Example:
result <- 1
i <- 1
repeat {
print(result)
i <- i + 1
result = result + 1
if(i > 5) {
break
}
}
Output:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
Function
A function should be
Syntax:
Function body
Example:
my_function <- function() {
print("Hello World!")
}
Output:
"Hello World!"
Function Components
The different parts of a function are −
Function Name − This is the actual name of the function. It is stored in R
environment as an object with this name.
Arguments − An argument is a placeholder. When a function is invoked, you
pass a value to the argument. Arguments are optional; that is, a function may
contain no arguments. Also arguments can have default values.
Function Body − The function body contains a collection of statements that
defines what the function does.
Return Value − The return value of a function is the last expression in the
function body to be evaluated.
R has many in-built functions which can be directly called in the program without
defining them first. We can also create and use our own functions referred as user
defined functions.
Return value
Function Types:
1. Math functions
2. Character functions
3. Statistical probability
4. Other statistical function
Output
[1] 4
2. sqrt(x) It returns the square root of input x. x<- 4
print(sqrt(x))
Output
[1] 2
3. ceiling(x) It returns the smallest integer which is x<- 4.5
larger than or equal to x. print(ceiling(x))
Output
[1] 5
4. floor(x) It returns the largest integer, which is x<- 2.5
smaller than or equal to x. print(floor(x))
Output
[1] 2
5. trunc(x) It returns the truncate value of input x. x<-
c(1.2,2.5,8.1)
print(trunc(x))
Output
[1] 1 2 8
6. round(x, It returns round value of input x. x<- 4.23456
digits=n) print(round(x,
digits=4)
Output
4.2346
7. cos(x), It returns cos(x), sin(x) value of input x<- 4
sin(x), x. print(cos(x))
tan(x) print(sin(x))
print(tan(x))
Output
[1] -06536436
[2] -0.7568025
[3] 1.157821
8. log(x) It returns natural logarithm of input x. x<- 4
print(log(x))
Output
[1] 1.386294
9. log10(x) It returns common logarithm of input x<- 4
x. print(log10(x))
Output
[1] 0.60206
10 exp(x) It returns exponent. x<- 4
print(exp(x))
Output
[1] 54.59815
Example:
# Create a sequence of numbers from 32 to 44.
print(seq(32,44))
Output:
[1] 32 33 34 35 36 37 38 39 40 41 42 43 44
[1] 53.5
[1] 1526
Output
[1] 1 3
3. sub(pattern, It finds pattern in x and st1<- "England is beautiful
replacement,x,ignore replaces it with replacement but not the part of EU"
case=FALSE, (new) text. sub("England', "UK", st1)
fixed=FALSE)
Output
[1] "UK is beautiful but not a
part of EU"
gsub(pattern, It finds every occurrence of st1<- "India is beautiful but
replacement,x,ignore pattern in x and replaces it India is not the part of
case=FALSE) with replacement (new) Europe"
text. sub("India”, "UK", st1)
Output
[1] " UK is beautiful but UK is
not the part of Europe "
4. paste(..., sep="") It concatenates strings after paste('one',2,'three',4,'five')
using sep string to separate
them. Output
[1] one 2 three 4 five
5. strsplit(x, split) It splits the elements of a<-"Split all the character"
character vector x at split print(strsplit(a, " "))
point.
Output
[[1]]
[1] "split" "all" "the"
"character"
6. tolower(x) It is used to convert the st1<- "shuBHAm"
string into lower case. print(tolower(st1))
Output
[1] shubham
7. toupper(x) It is used to convert the st1<- "shuBHAm"
string into upper case. print(toupper(st1))
Output
[1] SHUBHAM
User-defined Function
Example:
new.function <- function() {
for(i in 1:5) {
print(i^2)
}
}
new.function()
Output:
[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
Function calling with an argument:
Example:
new.function <- function(a) {
for(i in 1:a) {
b <- i^2
print(b)
}
}
new.function(6)
Output:
[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
[1] 36
Example:
my_function <- function(fname) {
paste(fname, "Griffin")
}
my_function("Peter")
my_function("Lois")
my_function("Stewie")
Output:
Example:
evenOdd = function(x){
if(x %% 2 == 0)
return("even")
else
return("odd")
}
print(evenOdd(4))
print(evenOdd(3))
Output:
[1] "even"
[1] "odd"
Example:
areaOfCircle = function(radius){
area = pi*radius^2
return(area)
}
print(areaOfCircle(2))
Output:
[1] 12.56637
Example:
new.function <- function(a,b,c) {
result <- a * b + c
print(result)
}
Output:
[1] 26
[1] 35
We can define the value of the arguments in the function definition and call
the function without supplying any argument to get the default result. But
we can also call such functions by supplying new values of the argument and
get non default result.
Example:
new.function <- function(a = 3, b = 6) {
result <- a * b
print(result)
}
new.function()
new.function(9,5)
Output:
[1] 45
Example:
Output:
Example:
Rectangle = function(length=5, width=4){
area = length * width
return(area)
}
print(Rectangle(2, 3))
print(Rectangle(width = 8, length = 4))
print(Rectangle())
Output:
[1] 6
[1] 32
[1] 20
Example:
new.function <- function(a, b) {
print(a^2)
print(a)
print(b)
}
new.function(6)
Output:
[1] 36
[1] 6
Error in print(b) : argument "b" is missing, with no default
Inline Functions:
To create an inline function you have to use the function command with
the argument x and then the expression of the function.
Example:
f = function(x) x^2*4+x/3
print(f(4))
print(f(-2))
print(0)
Output:
[1] 65.33333
[1] 15.33333
[1] 0
Recursive Functions: Recursion is when the function calls itself. This
forms a loop, where every time the function is called, it calls itself again
and again and this technique is known as recursion.
The use of recursion, often, makes the code shorter and it also looks
clean.
It is a simple solution for a few cases.
Example:
rec_fac <- function(x){
if(x==0 || x==1)
{
return(1)
}
else
{
return(x*rec_fac(x-1))
}
}
rec_fac(4)
Output:
[1] 24
Global Variables:
Example:
txt <- "awesome"
my_function <- function() {
paste("R is", txt)
}
my_function()
Output:
[1] "R is awesome"
If you create a variable with the same name inside a function, this
variable will be local, and can only be used inside the function. The
global variable with the same name will remain as it was, global and
with the original value.
Example:
txt <- "global variable"
my_function <- function() {
txt = "fantastic"
paste("R is", txt)
}
my_function()
txt
Output:
[1] "R is fantastic"
[1] "global variable"
To create a global variable inside a function, you can use the global
assignment operator <<-
Example:
my_function <- function() {
txt <<- "fantastic"
paste("R is", txt)
}
my_function()
print(txt)
Output:
[1] "R is fantastic"
[1] "fantastic"
Example:
txt <- "awesome"
my_function <- function() {
txt <<- "fantastic"
paste("R is", txt)
}
my_function()
Nested Functions:
sq.root(sq.root(25))
Output:
[1] 2.236068
Example:
Nested_function <- function(x, y) {
a <- x + y
return(a)
}
Nested_function(Nested_function(2,2), Nested_function(3,3))
Output:
[1] 10
You cannot directly call the Inner function because it has been defined
(nested) inside the Outer function.
Example:
Outer_func <- function(x) {
Inner_func <- function(y) {
a <- x + y
return(a)
}
}
z=Outer_func(5)
z(9)
Output:
[1] 14
Example:
num1 <- function(x) {
num2 <- function(y) {
return(x*y)
}
return(num2)
}
Output:
[1] 90