0% found this document useful (0 votes)
8 views

Introduction To R

Uploaded by

MEGHANA BONTHU
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Introduction To R

Uploaded by

MEGHANA BONTHU
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 91

• R is a popular programming language

used for statistical computing and


graphical presentation.

• Its most common use is to analyze and


visualize data.
• It is a great resource for data analysis, data
visualization, data science and machine learning
• It provides many statistical techniques (such as
statistical tests, classification, clustering and data
reduction)
• It is easy to draw graphs in R, like pie charts,
histograms, box plot, scatter plot, etc++
• It works on different platforms (Windows, Mac, Linux)
• It is open-source and free
• It has many packages (libraries of functions) that can
be used to solve different problems
KEYWORDS
Keyword is a reserved word defined for a specific
functionality
list of keywords:
function if else for
while repeat breaknext
true false NaN(not a number)
inf(infinite) NA(not available)
NA_real NA_integer NA_complex
NA_character NULL
variables
a variable is a memory location to store data
rules to be followed while naming variable:
• it should not match with keyword
• it can be alphanumeric
• it can have (.dot) or underscore
• no other symbol should be allowed
• name should start with alphabet but not digit
• it can also start with (.) but not followed by
number
• ASSIGNING VALUES TO THE
VARIABLE:
• assignment operator(=)
• eg: number=10
• using leftward operator(<-)
• eg: number <- 10
• using rightward operator(->)
• eg: 10->number
Data types in R
• LOGICAL-> TRUE OR FALSE
• INTEGER ->SET OF ALL INTEGERS
• NUMERIC -> SET OF ALL REAL
NUMBERS
• COMPLEX-> SET OF COMPLEX
NUMBERS
• CHARACTER -> “A…..Z”, “@”,”*”
# 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)
operators
• R supports different operators
• arthmetic operators
• relational operators
• logical operators
• assignment operators
• miscellaneous operators
•R Arithmetic Operators
•Arithmetic operators are used with numeric values to perform common
mathematical operations:
Operator Name Example
+ Addition x+y

- Subtraction x-y
* Multiplication x*y
/ Division x/y
^ Exponent x^y
%% Modulus (Remainder fro x %% y
m division)

%/% Integer Division x%/%y


R Assignment Operators:
• Assignment operators are used to assign values to
variables:
example:
my_var <- 3
my_var <<- 3
3 -> my_var
3 ->> my_var
my_var # print my_var
• <<- is a global assigner.
• R Comparison Operators
• Comparison operators are used to
compare two values:
Operator Name Example
== Equal x == y
!= Not equal x != y
> Greater than x>y
>= Greater than or x >= y
equal to
< Less than x<y
>= Greater than or x >= y
equal to
• R Logical Operators
• Logical operators are used to combine
conditional statements:
Operator Description
& Element-wise Logical AND operator. It returns TRUE if both
elements are TRUE
&& Logical AND operator - Returns TRUE if both statements are
TRUE
| Elementwise- Logical OR operator. It returns TRUE if one of the
statement is TRUE
|| Logical OR operator. It returns TRUE if one of the statement is
TRUE.
! Logical NOT - returns FALSE if statement is TRUE
• R Miscellaneous Operators
• Miscellaneous operators are used to
manipulate data:
Operator Description Example

: Creates a series of x <- 1:10


numbers in a sequence

%in% Find out if an element x %in% y


belongs to a vector
%*% Matrix Multiplication x <- Matrix1 %*%
Matrix2
DATA STRUCTURES IN R
• Data structures are the tools for holding
multiple values.
• vector
• List
• Array
• Matrices
• Data Frame
• Factors
• Vectors :
• A vector is simply a list of items that are of the
same type.
• It is a single dimensional data structure.
• To combine the list of items to a vector, use
the c() function and separate the items by a
comma.
EXAMPLE:
fruits <- c("banana", "apple", "orange")
fruits
OUTPUT:
"banana" "apple" "orange"
LISTS:
• A list in R can contain many different data types
inside it.
• it is also a single dimensional data structure.
• To create a list, use the list() function.
Example:
thislist <- list("apple", "banana", "cherry")
>thislist
Output:
“apple””banana””cherry”
Access Lists
• You can access the list items by referring
to its index number, inside brackets. The
first item has index 1, the second item has
index 2, and so on:
Example
thislist <- list("apple", "banana", "cherry")

thislist[1]
Output:
apple
Change Item Value
• To change the value of a specific item, refer to
the index number:
Example
thislist <- list("apple", "banana", "cherry")
thislist[1] <- "blackcurrant"

# Print the updated list


thislist
Output:
"blackcurrant“ "banana" "cherry"
List Length

• To find out how many items a list has, use


the length() function:
Example
thislist <- list("apple", "banana", "cherry")

length(thislist)
Output:
3
Check if Item Exists

• To find out if a specified item is present in a list, use


the %in% operator:
Example:
Check if "apple" is present in the list:
thislist <- list("apple", "banana", "cherry")

"apple" %in% thislist


Output:
[1] TRUE
Add List Items
• To add an item to the end of the list, use
the append() function:
Example
Add "orange" to the list:
thislist <- list("apple", "banana", "cherry")

append(thislist, "orange")
Output :
"apple“
"banana“
"cherry“
"orange"
• To add an item to the right of a specified
index, add "after=index number" in
the append() function:
Example:
Add "orange" to the list after "banana" (index
2):
thislist <- list("apple", "banana", "cherry")

append(thislist, "orange", after = 2)


Output:
"apple“ "banana" "orange“ "cherry"
Remove List Items
• You can also remove list items. The
following example creates a new, updated
list without an "apple" item:
Example
Remove "apple" from the list:
thislist <- list("apple", "banana", "cherry")
newlist <- thislist[-1]
newlist
Output:
"banana“ "cherry"
Range of Indexes
• You can specify a range of indexes by
specifying where to start and where to end the
range, by using the : operator:
Example
Return the second, third, fourth and fifth item:
thislist <-
list("apple", "banana", "cherry", "orange", "kiwi",
"melon", "mango")
(thislist)[2:5]
Output:
"banana“"cherry“"orange“ "kiwi"
Join Two Lists

The most common way is to use the c() function,


which combines two elements together:
Example:
list1 <- list("a", "b", "c")
list2 <- list(1,2,3)
list3 <- c(list1,list2)
list
Output:
"a”"b”"c”1 2 3
matrices
• In R, a two-dimensional rectangular data
set is known as a matrix. A matrix is
created with the help of the vector input to
the matrix function.
• In the R matrix, elements are arranged in
a fixed number of rows and columns.
• To create a matrix in R programming
lanaguage we use matrix() function
• matrix(data, nrow, ncol, byrow, dim_name)
• data
The first argument in matrix function is data. It is the input vector
which is the data elements of the matrix.
• nrow
The second argument is the number of rows which we want to
create in the matrix.
• ncol
The third argument is the number of columns which we want to
create in the matrix.
• byrow
The byrow parameter is a logical clue. If its value is true, then the
input vector elements are arranged by row.
• dim_name
The dim_name parameter is the name assigned to the rows and
columns.
• Example:
row_names = c("row1", "row2")
ccol_names = c("col1", "col2", "col3")
matrix1<-matrix(c(11, 13, 15, 12, 14, 16),nrow =2,
ncol =3, byrow = TRUE ,
dimnames = list(row_names, col_names))
matrix1
Output:
col1col2 col3
row1 11 13 15
row2 12 14 16
Accessing matrix elements in R

• Like C and C++, we can easily access the elements of


our matrix by using the index of the element. There are
three ways to access the elements from the matrix.
• We can access the element which presents on nth row
and mth column.
• We can access all the elements of the matrix which are
present on the nth row.
• We can also access all the elements of the matrix
which are present on the mth column.
Example
# Defining the column and row names.
row_names = c("row1", "row2", "row3", "row4")
ccol_names = c("col1", "col2", "col3")
#Creating matrix
R <- matrix(c(5:16), nrow = 4,ncol=3, byrow = TRUE, dimnames = list(row_names, col_na
mes))
print(R)

#Accessing element present on 3rd row and 2nd column


print(R[3,2])

#Accessing element present in 3rd row


print(R[3,])

#Accessing element present in 2nd column


print(R[,2])
Output:
col1 col2 col3
row1 5 6 7
row2 8 9 10
row3 11 12 13
row4 14 15 16
12
col1 col2 col3
11 12 13
row1 row2 row3 row4
6 9 12 15
Modification of the matrix

• R allows us to do modification in the


matrix. There are several methods to do
modification in the matrix, which are as
follows:
• Assign a single element
• Use of relational operations
• Addition of rows and columns
• Assign a single element
• In matrix modification, the first method is
to assign a single element to the matrix at
a particular position. By assigning a new
value to that position, the old value will get
replaced with the new one.
• The basic syntax for it is as follows:
• matrix[n, m]<-y
# Defining the column and row names.
row_names = c("row1", "row2", "row3", "row4")
ccol_names = c("col1", "col2", "col3")

R <- matrix(c(5:16), nrow = 4, byrow = TRUE, dimnames = list(


row_names, col_names))
print(R)

#Assigning value 20 to the element at 3d roe and 2nd column


R[3,2]<-20
print(R)
col1 col2 col3
row1 5 6 7
row2 8 9 10
Row3 11 12 13
Row4 14 15 16

col1 col2 col3


row1 5 6 7
row2 8 9 10
row3 11 20 13
row4 14 15 16
• Use of Relational Operator
• In this method, we used some relational
operators like >, <, ==.
• Let see an example to understand how
this method modifies the matrix.
• # Defining the column and row names.
• row_names = c("row1", "row2", "row3", "row4")
• ccol_names = c("col1", "col2", "col3")

R <- matrix(c(5:16), nrow = 4, byrow = TRUE, di
mnames = list(row_names, col_names))
• print(R)

• #Replacing element that equal to the 12
• R[R==12]<-0
• print(R)
col1 col2 col3
Row1 5 6 7
row2 8 9 10
row3 11 12 13
row4 14 15 16
col1 col2 col3
Row1 5 6 7
row2 8 9 10
row3 11 0 13
row4 14 15 16
• # Defining the column and row names.
• row_names = c("row1", "row2", "row3", "row4")
• ccol_names = c("col1", "col2", "col3")

• R <- matrix(c(5:16), nrow = 4, byrow = TRUE, dimnames
= list(row_names, col_names))
• print(R)

• #Replacing elements whose values are greater than 12
• R[R>12]<-0
• print(R)
col1 col2 col3
row1 5 6 7
row2 8 9 10
row3 11 12 13
row4 14 15 16

col1 col2 col3


Row1 5 6 7
row2 8 9 10
row3 11 12 0
row4 0 0 0
• Addition of Rows and Columns
• The third method of matrix modification is
through the addition of rows and columns
using the cbind() and rbind() function. The
cbind() and rbind() function are used to
add a column and a row respectively. Let
see an example to understand the working
of cbind() and rbind() functions.
• # Defining the column and row names.
• row_names = c("row1", "row2", "row3", "row4")
• ccol_names = c("col1", "col2", "col3")
• R <- matrix(c(5:16), nrow = 4, byrow = TRUE, dimnames = list(row_names, co
l_names))
• print(R)
• #Adding row
• rbind(R,c(17,18,19))
• #Adding column
• cbind(R,c(17,18,19,20))
• #transpose of the matrix using the t() function:
• t(R)
• #Modifying the dimension of the matrix using the dim() function
• dim(R)<-c(1,12)
• print(R)
col1 col2 col3
row1 5 6 7
row2 8 9 10
row3 11 12 13
row4 14 15 16 17 18 19
col1 col2 col3
row1 5 6 7 17
row2 8 9 10 18
row3 11 12 13 19
row4 14 15 16 20
row1 row2 row3 row4
col1 5 8 11 14
col26 9 12 15
col3 7 10 13 16
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 5 8 11 14 6 9 12 15 7 10 13 16
• Matrix operations
• In R, we can perform the mathematical
operations on a matrix such as addition,
subtraction, multiplication, etc. For
performing the mathematical operation on
the matrix, it is required that both the
matrix should have the same dimensions.
R <- matrix(c(5:16), nrow = 4,ncol=3)
S <- matrix(c(1:12), nrow = 4,ncol=3)
#Addition
sum<-R+S
print(sum)
#Subtraction
sub<-R-S
print(sub)
#Multiplication
mul<-R*S
print(mul)
#Multiplication by constant
mul1<-R*12
print(mul1)
#Division
div<-R/S
print(div)
ARRAYS
Arrays are the objects which allow us to
store data in more than two dimensions.
In R array is created with the help of the
array() functions
This array() function takes a vector as an
input and to create an array it uses vectors
values in the dim parameter
Example: if we create an array of dimension
(2,3,4) then it will create 4 rectangular
matrices of 2 row and 3 columns
Syntax of arrays
Name <- array(data,dim=(row_size,column_size,dim_names)

Example:
Vec1<- c(1,3,5)
Vec2 <- c(10,11,12,13,14,15)
Res <- array(c(vec1 , vec2),dim=c(3,3,2))
Print(res)
Output:
1 10 13
3 11 14
5 12 15

1 10 13
3 11 14
5 12 15
DATA FRAMES
• Data frames displays the data in the table
format
• Data frames can have different types of
data inside it
• Data.frame() funtion is used to create a
dataframe
• Syntax :- data.frame(arguments)
• Each column may be of different type, but
each row in the data frame must have the
same length
Example:
Data.frame(a=c(10,20,30,40),b=(10,20,30))
Error
In data frames columns are called variable
Example:
>companies<-data.frame(mobiles=c(“oppo”,”
apple”),rank=c(1,2))
>companies
Output:
mobiles rank
oppo 1
apple 2
ACCESSING THE ELEMENTS:
• Data frame elements can be accessed
using
• Single square bracket []
• Double square brackes [[]]
• Using $
Example:
• Companies[1]
• Companies[[“mobiles”]]
• Companies$mobiles
ADDING ROWS AND COLUMNS
• Rows are added using function rbind(),columns
are added using the function cbind()
Example:
#adding row
Newrow<-rbind(companies,c(“LG”,3)
Newrow
mobiles rank
oppo 1
apple 2
LG 3
#example to add column:
Newcol<-
cbind(companies,price=(30000,60000))
Output:
mobile rank price
oppo 1 30000
apple 2 60000
FACTORS :
In R factors are used to categorize the data
Example:
Music:classic, rock, pop
The function factor() is used to create a factor
Example:
music<-
factor(c(“classic”,”pop”,”rock”,”classic”,”rock”,”pop”,”others”))
>music
Output:
classic,pop,rock,classic,rock,pop,others
Levels: “classic”,”pop”,”rock”,”others”
To print only levels we can use levels() function
Levels: “classic”,”pop”,”rock”,”others”
ACCESSING FACTORS:

To access the items in a factor we use


index number using [] brackets
Example:
Music[2]
Output
“pop”
USER-DEFINED FUNCTIONS
IN R
• A function is a block of code to perform a task.
Function is executed when it is called
• The keyword function is used to create any user
defined functions in R.
• The basic structure of the function is
function_name <- function(arg1,arg2..)
{
function body
return
}
• You can create a function in 4 ways
• Function with no argument no return
• Function with no argument with return
• Function with with argument no return
• Function with with argument with return
Function with no argument no return
Example:
Sum1 <- function()
{
a<-10
b<-20
c<-a+b
print(“addition of 2 numbers is”)
print(c)
}
Output:
>sum1()
addition of 2 numbers is
30
Function with no argument with return
Example:
Sum2 <- funtion()
{
a<- 10
b<- 20
c<- a+b
print(“addition of 2 numbers is”)
return( C )
}
Output:
> sum2()
addition of 2 numbers is
30
Function with with argument no return
Example:
sum3 <- function(a,b)
{
c <- a+b
print(“addition of 2 numbers is”)
printf(c)
}
Output
> sum3(10,20)
addition of 2 numbers is
30
Function with with argument with return
Example:
Sum4 <- function(a,b)
{
c<- a+b
print(“addition of 2 numbers is “)
return(c)
}
Output:
> sum4(10,20)
addition of 2 numbers is
30
SCRIPTS:
• A script is common text file containing
commands or statements for doing a task.
• Script file is saved with “ .r” extension
• Functions can also save in the script file
• Creating and running script files:
• to create a script file user can use any type of
text editor eg:notepad
• The source command is used to run the script
files by passing full path of the file
• Eg : source(“d:\\sample\\demo.r”)
R Built-in functions
• Math Functions
• string functions
• statistical functions
• statistical probability functions
math functions
• abs(x)-It returns the absolute value of
input x.
• sqrt(x)-It returns the square root of input x.
• ceiling(x)-It returns the smallest integer
which is larger than or equal to x.
• floor(x)-It returns the largest integer, which
is smaller than or equal to x.
• trunc(x)-It returns the truncate value of
input x.
• round(x, digits=n)-It returns round value of
input x.
• cos(x), sin(x), tan(x)-It returns cos(x),
sin(x) value of input x.
• log(x)-It returns natural logarithm of input
x.
• log10(x)-It returns common logarithm of
input x.
• exp(x)-It returns exponent.
String Functions
• R provides various string functions to
perform tasks. These string functions allow
us to extract sub string from string, search
pattern etc. There are the following string
functions in R:
• substr(x, start=n1,stop=n2)-It is used to
extract substrings in a character vector.
• grep(pattern, x)-It searches for pattern in
x.
• sub(pattern, replacement, x)-It finds
pattern in x and replaces it with
replacement (new) text.
• paste(string1,string2, sep=delimiters)-It
concatenates strings after using sep string
to separate them.
• strsplit(x, split)-It splits the elements of
character vector x at split point.
• tolower(x)-It is used to convert the string
into lower case.
• toupper(x)-It is used to convert the string
into upper case.
Statistical Function
• mean(x)-It is used to find the mean for x
object
• sd(x)-It returns standard deviation of an
object.
• median(x)-It returns median.
• range(x)-It returns range.
• sum(x)-It returns sum.
• diff(x)
• min(x),max(x)
INPUT AND OUTPUT
FUNCTIONS
• INPUT FUCTIONS:
• readline() : always reads a single value as
a character data type.
• syntax:
• var_name=readline()
• var_name=readline(“enter a value”)
• var_name=readline(prompt=”enter a
value”)
• scan(): reads multiple values and it forms
a vector
• it does not take any parameters
• syntax:
• var_name=scan()
• output functions:
• print() : used to print string constants
• it takes only one parameter
• it does not concatenate multiple outputs.
• cat():
• concatenate multiple outputs
CONDITIONAL STATEMENTS
• R language has conditions statements for
decision making
• Logical and relational operators are used
for combining more than one condition in
the conditional statements
• Conditional statements in R:
• IF statement
• IF – ELSE statement
• ELSE-IF LADDER
IF statement
The if statement takes one or more
condition and executes the given
statements if the condition is true.
Syntax:
If(condition)
{
statements
}
example:
N <- readline(“enter any number”)
N <- as.integer(N)
If( N > 0)
Print(“ entered number is a positive number”)
Output:
Source(“path”)
Enter any number
5
Entered number is a positive number
• If…else statement :
• The if…else statement also takes one or more conditions. The
conditions are true it executes the statements in if part other wise it
executes the statements in else part
Syntax:
if (condition)
{
statements
}
Else
{
statements
}
Example:
N <- readline(“enter a number”)
N <- as.integer(N)
If( (N %% 2)==0)
{
print(“entered number is even”)
}
Else
{
print(“entered number is odd”)
}
if…else ladder statement:
the nested if…else statement is used when the user needs to check further
conditions after checking some prior conditions.
Syntax:
If(condition)
{
statements
}
else if( condition)
{
statements
}
else
{
statements
}
Example:
a <- readline(“enter first number”)
a <- as.integer(a)
b<- readline(“enter second number”)
b <- as.integer(b)
c <- readline(“enter third number”)
c <- as.integer(c)
If ((a>b) && (a>c))
{
print(“first number is greater”)
}
Else if (b >c)
{
print(“second number is greater “)
}
Else
{
Print(“third number is greater “)
• }
LOOPS IN R
• Some times to complete a task or
mathematical calculation, the user needs
to execute a single statement or group of
statement multiple times
• R has three types of loops
• 1.for loop
• 2.while loop
• repeat
FOR LOOP :
• The for loop executes either a single statement or
multiple statements repeatedly.
• Syntax :
• For ( name in expression)
• {
• statements;
• }
• Name is loop variable;
• Expression is a vector sequence
• Name takes the values of this sequence during
looping
Example:
For( I in 1:10)
{
print(paste(“i= “,i))
}
Output:
i=1
1=2….
i=10
While loop
• The while loop checks the given condition and
executes a single statement or multiple statements
repeatedly until the given condition is met.
• Syntax:
• Loop variable initialization
• While(condition)
• {
• statements
• loop variable increment/ decrement
• }
Example:
i <- 1
while( i<10)
{
print( paste(“i= “,i))
i <- i+1
}
Repeat loop
• The repeat loop is similar to the while loop except that it has no
condition.the repeat loop repeatedly checks single or multiple
statements and goes into an infinite loop.
• It needs a break statement to terminate the continued execution of
a loop.
• Syntax:
• Loop variable initialization
• Repeat
• {
• statements
• loop variable increment/decrement
• break statement
• }
Example:
i<-1
repeat
{
print(paste(“i= “,i))
i=i+1
if(i==10)
{
break
}
}
Loop control statements
• Break statement:
• the break statement terminates the loop
at some point. It is defined inside a loop
that terminates the iteration and passes
the control to the outside of loop
• Next statement :
• The next statement continues the
execution of the loop without terminating it.
switch
• switch case statements compares a
variable against a list of values
• syntax:
• switch(expression,case1,case2,case3...)
• example:
• var1=10
• var2=2
• var3=”s”
• result=switch(var3,

“a”=cat(“addition=”,var1+var2),
“s”=cat(“subtraction=”,var1-var2))
print(result)

You might also like