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

Introduction To R - Part1

R is a programming language and software environment for statistical analysis, graphics representation and reporting. It allows branching and looping as well as modular programming using functions. R allows integration with procedures written in other languages like C, C++, Java, or Python. R provides tools for data analysis, graphical facilities for data display, and handles different data types like vectors, lists, matrices, arrays, and factors.

Uploaded by

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

Introduction To R - Part1

R is a programming language and software environment for statistical analysis, graphics representation and reporting. It allows branching and looping as well as modular programming using functions. R allows integration with procedures written in other languages like C, C++, Java, or Python. R provides tools for data analysis, graphical facilities for data display, and handles different data types like vectors, lists, matrices, arrays, and factors.

Uploaded by

Mohamed S Saleh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Introduction to R

R - Overview
• R is a programming language and software environment for statistical
analysis, graphics representation and reporting.
• The core of allows branching and looping as well as modular
programming using functions.
• R allows integration with the procedures written in the C, C++, Java, or
Python.
• R is freely available.
Features of R
• R is a well-developed, simple and effective programming language
which includes conditionals, loops, user defined recursive functions
and input and output facilities.
• R has an effective data handling and storage facility.
• R provides operators for calculations on arrays, lists, vectors and
matrices.
• R provides a large collection of tools for data analysis.
• R provides graphical facilities for data analysis and display.
Local Environment Setup
• Download R-core.exe file:
• https://cran.r-project.org/bin/windows/base/

• Download Rstudio:
• https://www.rstudio.com/products/rstudio/download/
R - Data Types
Data Type Example Code
Logical TRUE, FALSE v<-TRUE
print(class(v))
[1] "logical"

Numeric 12.3, 5, 999 v<-23.5


print(class(v))
[1] "numeric"

Integer 2L, 34L, 0L V<- 2L


print(class(v))
[1] "integer"
R - Data Types
Data Type Example Code
Complex 3 + 2i v <- 2+5i
print(class(v))
[1] "complex"

Character 'a' , '"good", "TRUE", '23.4' v <- "TRUE"


print(class(v))
[1] "character"

Raw "Hello" is stored as 48 65 6c v <- charToRaw("Hello")


6c 6f print(class(v))
[1] "raw"
R - Data Types (Vectors)
• Vector: Basic R-object with more than one element.
• Use c() function which means to combine the elements into a vector.

Example Output
# Create a vector. [1] "red" "green" "yellow"
apple <- c('red','green',"yellow") [1] "character"
print(apple)
# Get the class of the vector.
print(class(apple))
R - Data Types (Lists)
• A list: an R-object which can contain many
different types of elements inside it like vectors,
functions and even another list inside it.
Example Output
# Create a list. [[1]]
list1 <- list(c(2,5,3),21.3,sin) [1] 2 5 3
# Print the list. [[2]]
print(list1) [1] 21.3
[[3]]
function (x) .Primitive("sin")
R - Data Types (Matries)
• A matrix: is a two-dimensional rectangular data set.
• It can be created using a vector input to the matrix function.
Example Output
# Create a matrix. [,1] [,2] [,3]
M = matrix( c('a','a','b','c','b','a'), nrow = [1,] "a" "a" "b"
2, ncol = 3, byrow = TRUE) [2,] "c" "b" "a"
print(M)
R - Data Types (Arrays)
• Arrays: can be of any number of dimensions.
• dim attribute which creates the required number of dimension.
Example Output
# Create an array with two elements ,,1
which are 3x3 matrices each. [,1] [,2] [,3]
[1,] "green" "yellow" "green"
a <- array(c('green','yellow'),dim = [2,] "yellow" "green" "yellow"
c(3,3,2)) [3,] "green" "yellow" "green"
print(a) ,,2
[,1] [,2] [,3]
[1,] "yellow" "green" "yellow"
[2,] "green" "yellow" "green"
[3,] "yellow" "green" "yellow"
R - Data Types (Factors)
• Factor: stores the vector with distinct values of the elements.
• Factors are created using the factor() function.
• The nlevels functions gives the count of levels.
Example Output
# Create a vector. [1] green green yellow red red red
apple_colors <- green
c('green','green','yellow','red','red','red',' Levels: green red yellow
green') [1] 3
# Create a factor object.
factor_apple <- factor(apple_colors)
# Print the factor.
print(factor_apple)
print(nlevels(factor_apple))
R - Data Types (Data Frames)
• Data frames: are tabular data objects.
• Unlike a matrix in data frame each column can contain different modes of data.
• The first column can be numeric while the second column can be character and
third column can be logical.
• It is a list of vectors of equal length.
Example Output
# Create the data frame. gender height weight Age
BMI <- data.frame( 1 Male 152.0 81 42
gender = c("Male", "Male","Female"), 2 Male 171.5 93 38
height = c(152, 171.5, 165), 3 Female 165.0 78 26
weight = c(81,93, 78),
Age = c(42,38,26)
)
print(BMI)
R - Variables
• A variable in R can store an atomic vector, group of atomic vectors or
a combination of many R objects.
• A valid variable name consists of letters, numbers and the dot or
underline characters.
• The variable name starts with a letter or the dot not followed by a
number.
Variable Assignment
• The cat() function combines multiple items into a continuous print output.
Example Output
# Assignment using equal operator. [1] 0 1 2 3
var.1 = c(0,1,2,3) var.1 is 0 1 2 3
# Assignment using leftward operator. var.2 is learn R
var.2 <- c("learn","R") var.3 is 1 1
# Assignment using rightward operator.
c(TRUE,1) -> var.3
print(var.1)
cat ("var.1 is ", var.1 ,"\n")
cat ("var.2 is ", var.2 ,"\n")
cat ("var.3 is ", var.3 ,"\n")

• 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.
R - Operators
• An operator is a symbol that tells the compiler to perform specific
mathematical or logical manipulations.
• R language is rich in built-in operators and provides following types of
operators.
• Types of Operators
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Assignment Operators
• Miscellaneous Operators
Arithmetic Operators
Operator Description Example
+ Adds two vectors v <- c(2, 5.5, 6) t <- c(8, 3, 4)
print(v+t)
[1] 10.0 8.5 10.0

− Subtracts second vector from the v <- c( 2,5.5,6) t <- c(8, 3, 4)


first print(v-t)
[1] -6.0 2.5 2.0

* Multiplies both vectors v <- c( 2,5.5,6) t <- c(8, 3, 4)


print(v*t)
[1] 16.0 16.5 24.0
Arithmetic Operators
Operator Description Example
/ Divide the first vector with the second v <- c( 2,5.5,6) t <- c(8, 3, 4)
print(v/t)
[1] 0.250000 1.833333 1.500000

%% Give the remainder of the first vector v <- c( 2,5.5,6) t <- c(8, 3, 4)
with the second print(v%%t )
[1] 2.0 2.5 2.0

%/% The result of division of first vector v <- c(2, 5.5, 6) t <- c(8, 3, 4)
with second (quotient) print(v%/%t)
[1] 0 1 1
Arithmetic Operators
Operator Description Example
^ The first vector raised to the exponent v <- c( 2,5.5,6) t <- c(8, 3, 4)
of second vector print(v^t)
[1] 256.000 166.375 1296.000
Relational Operators
• Each element of the first vector is compared with the corresponding
element of the second vector.
• The result of comparison is a Boolean value.
Operator Description Example
> v <- c(2,5.5,6,9) t <- c(8,2.5,14,9)
Checks if each element of the first print(v>t)
vector is greater than the [1] FALSE TRUE FALSE FALSE
corresponding element of the
second vector.

< v <- c(2,5.5,6,9) t <- c(8,2.5,14,9)


Checks if each element of the first print(v < t)
vector is less than the [1] TRUE FALSE TRUE FALSE
corresponding element of the
second vector.
Relational Operators
Operator Description Example
== Checks if each element of the first
v <- c(2,5.5,6,9) t <- c(8,2.5,14,9)
print(v == t)
vector is equal to the corresponding [1] FALSE FALSE FALSE TRUE
element of the second vector.
<= Checks if each element of the first v <- c(2,5.5,6,9) t <- c(8,2.5,14,9) print(v<=t)
vector is less than or equal to the [1] TRUE FALSE TRUE TRUE
corresponding element of the second
vector.
>= Checks if each element of the first v <- c(2,5.5,6,9) t <- c(8,2.5,14,9) print(v>=t)
vector is greater than or equal to the [1] FALSE TRUE FALSE TRUE
corresponding element of the second
vector.
!= v <- c(2,5.5,6,9) t <- c(8,2.5,14,9) print(v!=t)
Checks if each element of the first [1] TRUE TRUE TRUE FALSE
vector is unequal to the corresponding
element of the second vector.
Logical Operators
• Each element of the first vector is compared with the corresponding
element of the second vector. The result is a Boolean value.
Operator Description Example
& v <- c(3,1,TRUE,2+3i)
t <- c(4,1,FALSE,2+3i)
Logical AND operator. print(v&t)
[1] TRUE TRUE FALSE TRUE

| v <- c(3,0,TRUE,2+2i)
t <- c(4,0,FALSE,2+3i)
Element-wise Logical OR operator. print(v|t)
[1] TRUE FALSE TRUE TRUE

! v <- c(3,0,TRUE,2+2i)
It is called Logical NOT operator. print(!v)
[1] FALSE TRUE FALSE FALSE
Assignment Operators
• These operators are used to assign values to vectors.
Operator Description Example
Called Left Assignment v1 <- c(3,1,TRUE,2+3i)
<− v2 <<- c(3,1,TRUE,2+3i)
or v3 = c(3,1,TRUE,2+3i)
= print(v1) print(v2) print(v3)
or [1] 3+0i 1+0i 1+0i 2+3i
<<− [1] 3+0i 1+0i 1+0i 2+3i
[1] 3+0i 1+0i 1+0i 2+3i

Called Right Assignment c(3,1,TRUE,2+3i) -> v1


c(3,1,TRUE,2+3i) ->> v2
-> print(v1) print(v2)
or [1] 3+0i 1+0i 1+0i 2+3i
->> [1] 3+0i 1+0i 1+0i 2+3i
Miscellaneous Operators
• These operators are used to for specific purpose and not general mathematical or
logical computation.
Operator Description Example
: v <- 2:8 print(v)
Colon operator. It creates the series of [1] 2 3 4 5 6 7 8
numbers in sequence for a vector.

%in% v1 <- 8 v2 <- 12 t <- 1:10


This operator is used to identify if an print(v1 %in% t)
element belongs to a vector. print(v2 %in% t)
[1] TRUE [1] FALSE
%*% M = matrix( c(2,6,5,1,10,4), nrow = 2,ncol =
3,byrow = TRUE) t = M %*% t(M)
This operator is used to multiply a matrix print(t)
with its transpose. [,1] [,2] [1,] 65 82 [2,] 82 117
R - Functions
• R has a large number of built-in functions and the user can create
their own functions.
• Function Components
• Function Name 
• Arguments 
• Function Body 
• Return Value
R - Built-in Function
• Simple examples of built-in functions
are seq(), mean(), max(), sum(x), etc.
• They are directly called by user written programs.
Example Output
# Create a sequence of numbers from 32 to [1] 32 33 34 35 36 37 38 39 40 41 42 43 44
44. [1] 53.5
print(seq(32,44)) [1] 1526

# Find mean of numbers from 25 to 82.


print(mean(25:82))

# Find sum of numbers frm 41 to 68.


print(sum(41:68))
User-defined Function
• They are specific to what a user wants and once created they can be
used like the built-in functions.
Example Output
# Create a function to print squares of numbers in [1] 1
sequence. [1] 4
new.function <- function(a) { [1] 9
for(i in 1:a) { [1] 16
b <- i^2 [1] 25
print(b) [1] 36
}
}
# Call the function new.function supplying 6 as an
argument.
new.function(6)
User-defined Function
• They are specific to what a user wants and once created they can be
used like the built-in functions.
Example Output
# Create a function with arguments. [1] 26
new.function <- function(a,b,c) { [1] 58
result <- a * b + c print(result)
}

# Call the function by position of arguments.


new.function(5,3,11)

# Call the function by names of the arguments.


new.function(a = 11, b = 5, c = 3)
User-defined Function
• They are specific to what a user wants and once created they can be
used like the built-in functions.
Example Output
# Create a function with arguments. [1] 18
new.function <- function(a = 3, b = 6) { [1] 45
result <- a * b print(result)
}

# Call the function without giving any argument.


new.function()

# Call the function with giving new values of the argument.


new.function(9,5)
User-defined Function
• They are specific to what a user wants and once created they can be
used like the built-in functions.
Example Output
# Create a function with arguments. [1] 36
new.function <- function(a, b) { [1] 6
print(a^2) print(a) print(b) Error in print(b) : argument "b" is
} missing, with no default

# Evaluate the function without supplying one of the


arguments.
new.function(6)
R - Strings
• Any value written within a pair of single quote or double quotes in R is
treated as a string.
Example Output
a <- "Hello" [1] "Hello How are you? "
b <- 'How’ [1] "Hello-How-are you? "
c <- "are you? " [1] "HelloHoware you? "
print(paste(a,b,c))
print(paste(a,b,c, sep = "-"))
print(paste(a,b,c, sep = "", collapse = ""))
R - Strings
Example Output
result <- nchar("Count the number of characters") [1] 30
print(result)
# Changing to Upper case. [1] "CHANGING TO UPPER"
result <- toupper("Changing To Upper") [1] "changing to lower"
print(result)

# Changing to lower case.


result <- tolower("Changing To Lower")
print(result)
# Extract characters from 5th to 7th position. [1] "act"
result <- substring("Extract", 5, 7)
print(result)
Packages
• Packages are collections of R functions, data, and compiled code in a well-
defined format.
• The directory where packages are stored is called the library.
• R comes with a standard set of packages.
• Others are available for download and installation.
• Once installed, they have to be loaded into the session to be used.

• .libPaths() # get library location


• library() # see all packages installed
• search() # see packages currently loaded
Getting Help
• Once R is installed, there is a comprehensive built-in help system.
• At the program's command prompt you can use any of the following:

• help.start() # general help


• help(foo) # help about function foo
• ?foo # same thing
• apropos("foo") # list all functions containing string foo
• example(foo) # show an example of function foo
Setting The Working Directory In R
• setwd() – Set or Change R Working Directory
• setwd("/my/new/path)

• getwd – get current working directory


• getwd()

34

You might also like