0% found this document useful (0 votes)
6 views121 pages

Introd R

The document is an introduction to R programming, covering fundamental concepts such as arithmetic operations, vectors, matrices, control structures, and functions. It provides practical examples and explanations of various data types, including strings and boolean operations, as well as data manipulation techniques like filtering and data imputation. The document serves as a comprehensive guide for beginners to understand and utilize R for statistical analysis.
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)
6 views121 pages

Introd R

The document is an introduction to R programming, covering fundamental concepts such as arithmetic operations, vectors, matrices, control structures, and functions. It provides practical examples and explanations of various data types, including strings and boolean operations, as well as data manipulation techniques like filtering and data imputation. The document serves as a comprehensive guide for beginners to understand and utilize R for statistical analysis.
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/ 121

Introduction to R

Eduardo Martínez

2022-09-30

Eduardo Martínez Introduction to R 2022-09-30 1 / 121


1 Arithmetic in R

2 Vectors and Matrices

3 Control Structures

4 Functions

5 DataFrame

6 Graphs - ggplot2

Eduardo Martínez Introduction to R 2022-09-30 2 / 121


R Markdown

Free software to perform statistical procedures.


Rstudio is an IDE for R.

Eduardo Martínez Introduction to R 2022-09-30 3 / 121


Section 1

Arithmetic in R

Eduardo Martínez Introduction to R 2022-09-30 4 / 121


Basic operations

3+2-10ˆ2

## [1] -95
3+(2-10)ˆ2

## [1] 67
sqrt(16)

## [1] 4
sqrt(2)

## [1] 1.414214

Eduardo Martínez Introduction to R 2022-09-30 5 / 121


Basic operations

3+2-10ˆ2

## [1] -95
3+(2-10)ˆ2

## [1] 67
sqrt(16)

## [1] 4
sqrt(2)

## [1] 1.414214

Eduardo Martínez Introduction to R 2022-09-30 6 / 121


Assigning values to a variable

var1 <- 3
var1

## [1] 3
var2 = 4
var2

## [1] 4
var1ˆ2+var2ˆ2

## [1] 25

Eduardo Martínez Introduction to R 2022-09-30 7 / 121


Functions

sin(3.14)

## [1] 0.001592653
acos(-1)

## [1] 3.141593
exp(1)

## [1] 2.718282

Eduardo Martínez Introduction to R 2022-09-30 8 / 121


Logarithms

log(2) # (ln)

## [1] 0.6931472
log10(2) # log base 10

## [1] 0.30103
log(16,base = 2)

## [1] 4

Eduardo Martínez Introduction to R 2022-09-30 9 / 121


Undefined values (mistakes)

log(0)

## [1] -Inf
1/0

## [1] Inf
0/0

## [1] NaN

Eduardo Martínez Introduction to R 2022-09-30 10 / 121


Boolean operations

value1 = TRUE

value1

## [1] TRUE
class(value1)

## [1] "logical"
value2 = FALSE

Eduardo Martínez Introduction to R 2022-09-30 11 / 121


Boolean operations

value1 && value2 #and

## [1] FALSE
value1 || value2 #or

## [1] TRUE
!value1 #not

## [1] FALSE

Eduardo Martínez Introduction to R 2022-09-30 12 / 121


Functions with boolean output

4 >= 2

## [1] TRUE
var1 != var2 # útil para filtrar

## [1] TRUE
value1 != value2

## [1] TRUE

Eduardo Martínez Introduction to R 2022-09-30 13 / 121


Strings

text1 = "Hello world"

text1

## [1] "Hello world"


class(text1)

## [1] "character"

Eduardo Martínez Introduction to R 2022-09-30 14 / 121


Strings

toupper(text1) # útil para limpiar

## [1] "HELLO WORLD"


tolower(text1) # útil para limpiar

## [1] "hello world"

Eduardo Martínez Introduction to R 2022-09-30 15 / 121


Subsets of a string

substring(text1, 1, 4)

## [1] "Hell"
substring(text1, 6, 10)

## [1] " worl"


substring(text1, 5, 5)

## [1] "o"

Eduardo Martínez Introduction to R 2022-09-30 16 / 121


Replacements

gsub("e","i", text1)

## [1] "Hillo world"


gsub("d","t", text1)

## [1] "Hello worlt"


text2 = "it's me."

paste(text1,text2)

## [1] "Hello world it's me."

Eduardo Martínez Introduction to R 2022-09-30 17 / 121


Section 2

Vectors and Matrices

Eduardo Martínez Introduction to R 2022-09-30 18 / 121


Vectors

vec1 <- c(8,6,7,5,3,0,9)

vec1[1] # R index starts at 1

## [1] 8
vec1[7]

## [1] 9
vec1[10]

## [1] NA

Eduardo Martínez Introduction to R 2022-09-30 19 / 121


Calling the last element

length(vec1)

## [1] 7
vec1[length(vec1)]

## [1] 9

Eduardo Martínez Introduction to R 2022-09-30 20 / 121


Slicing vectors with [ ]

vec1[2:5]

## [1] 6 7 5 3
vec1[5:10]

## [1] 3 0 9 NA NA NA
vec1[c(2,4,6)]

## [1] 6 5 0
vec1[c(2,4,6,8)]

## [1] 6 5 0 NA

Eduardo Martínez Introduction to R 2022-09-30 21 / 121


Operations

vec2 = c(5,3,8,2,10,0,1)

vec3 = c(3, TRUE, 4, "two", 3, 8, 2)

vec4 = c(3, NA, 4, NA, 3, 8, 2)

Eduardo Martínez Introduction to R 2022-09-30 22 / 121


Operations

vec1 + vec2
vec1 + vec3
vec1 + vec4

Eduardo Martínez Introduction to R 2022-09-30 23 / 121


Vectors from a sequence

vec5 = seq(2, 16, by = 2)

vec6 = seq(50, 20, by = -2)

vec7 = seq(0, 1, by = 0.01)

Eduardo Martínez Introduction to R 2022-09-30 24 / 121


Operations

vec1 - vec2

## [1] 3 3 -1 3 -7 0 8
vec1*vec2

## [1] 40 18 56 10 30 0 9
vec1 %*% vec2

## [,1]
## [1,] 163
vec1 / vec2

## [1] 1.600 2.000 0.875 2.500 0.300 NaN 9.000

Eduardo Martínez Introduction to R 2022-09-30 25 / 121


Operations

vec1 + vec3
vec1 + vec4
vec1 + vec5

Eduardo Martínez Introduction to R 2022-09-30 26 / 121


Operations

vec1 + 3

## [1] 11 9 10 8 6 3 12
vec1 + c(3,3,3,3,3,3,3)

## [1] 11 9 10 8 6 3 12
vec5 + c(2,3)

## [1] 4 7 8 11 12 15 16 19
vec1 + c(2,3)

## Warning in vec1 + c(2, 3): longer object length is not a mu


## object length
## [1] 10 9 9 8 5 3 11

Eduardo Martínez Introduction to R 2022-09-30 27 / 121


Deleting odd positions of vec5

vec5 * c(0,1)

## [1] 0 4 0 8 0 12 0 16

Eduardo Martínez Introduction to R 2022-09-30 28 / 121


Vectorization

vec1ˆ2

## [1] 64 36 49 25 9 0 81
vec1ˆvec2

## [1] 32768 216 5764801 25 59049 1 9

Eduardo Martínez Introduction to R 2022-09-30 29 / 121


Vectorization

sqrt(vec1)

## [1] 2.828427 2.449490 2.645751 2.236068 1.732051 0.000000 3


exp(vec1)

## [1] 2980.95799 403.42879 1096.63316 148.41316 20.08554


log(vec1)

## [1] 2.079442 1.791759 1.945910 1.609438 1.098612 -Inf 2

Eduardo Martínez Introduction to R 2022-09-30 30 / 121


Getting information of a vector

min(vec1)

## [1] 0
max(vec1)

## [1] 9
sum(vec1)

## [1] 38

Eduardo Martínez Introduction to R 2022-09-30 31 / 121


Getting information of a vector

mean(vec1)

## [1] 5.428571
var(vec1)

## [1] 9.619048
sd(vec1)

## [1] 3.101459

Eduardo Martínez Introduction to R 2022-09-30 32 / 121


Getting information of a vector

summary(vec1)

## Min. 1st Qu. Median Mean 3rd Qu. Max.


## 0.000 4.000 6.000 5.429 7.500 9.000
sort(vec2) # de menor a mayor

## [1] 0 1 2 3 5 8 10
sort(vec2, decreasing = TRUE) # de mayor a menor

## [1] 10 8 5 3 2 1 0

Eduardo Martínez Introduction to R 2022-09-30 33 / 121


Funciones sobre vectores “raros”

sumar caracteres sum(vec3)


sumar NA sum(vec4)
sum(vec4, na.rm = TRUE) ## na.remove
vec1 + vec4

Eduardo Martínez Introduction to R 2022-09-30 34 / 121


Boolean vectors

vec8 = c(TRUE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE)

sum(vec8)

## [1] 3
vec1 + vec8

## [1] 9 7 7 5 3 1 9
mean(vec8)

## [1] 0.4285714

Eduardo Martínez Introduction to R 2022-09-30 35 / 121


Finding NA

is.na(vec4)

## [1] FALSE TRUE FALSE TRUE FALSE FALSE FALSE


sum(is.na(vec4)) # counting NA

## [1] 2
sum(!is.na(vec4)) # counting not NA

## [1] 5

Eduardo Martínez Introduction to R 2022-09-30 36 / 121


Boolean output with vectors

vec1 > 5

## [1] TRUE TRUE TRUE FALSE FALSE FALSE TRUE


sum(vec1 > 5)

## [1] 4
!(vec1 > 5)

## [1] FALSE FALSE FALSE TRUE TRUE TRUE FALSE


sum(vec1 <=5)

## [1] 3

Eduardo Martínez Introduction to R 2022-09-30 37 / 121


Using [] and boolean sentences to filter

is.na(vec4)

## [1] FALSE TRUE FALSE TRUE FALSE FALSE FALSE


vec4[is.na(vec4)] # NA values

## [1] NA NA
vec4[!is.na(vec4)] # not NA values

## [1] 3 4 3 8 2

Eduardo Martínez Introduction to R 2022-09-30 38 / 121


Using [] and boolean sentences to filter

vec1 > 5

## [1] TRUE TRUE TRUE FALSE FALSE FALSE TRUE


vec1[vec1 > 5]

## [1] 8 6 7 9
vec1[vec1 < 8]

## [1] 6 7 5 3 0

Eduardo Martínez Introduction to R 2022-09-30 39 / 121


Using a vector to slice another one

vec1 %% 2 == 0 # TRUE if the entry is even

## [1] TRUE TRUE FALSE FALSE FALSE TRUE FALSE


vec2[vec1 %% 2 == 0] # if the entry in vec1 is even, show the

## [1] 5 3 0
vec4[vec1 %% 2 == 0]

## [1] 3 NA 8

Eduardo Martínez Introduction to R 2022-09-30 40 / 121


Using a vector to slice another one

vec1[vec4 %% 2 == 0]

## [1] NA 7 NA 0 9
vec5[vec1 %% 2 == 0] # be careful

## [1] 2 4 12 16
vec6[vec1 %% 2 == 0] # be careful

## [1] 50 48 40 36 34 26 22 20

Eduardo Martínez Introduction to R 2022-09-30 41 / 121


Assigning values

vec2[5]

## [1] 10
vec2[5] = 1

vec1[4] = 6

Eduardo Martínez Introduction to R 2022-09-30 42 / 121


Data imputation

vec4[is.na(vec4)] = 0

Eduardo Martínez Introduction to R 2022-09-30 43 / 121


Data imputation

vec4 = c(3, NA, 4, NA, 3, 8, 2)

vec4[!is.na(vec4)] # not null values in vec4

## [1] 3 4 3 8 2
mean(vec4[!is.na(vec4)])

## [1] 4

Eduardo Martínez Introduction to R 2022-09-30 44 / 121


Data imputation

vec4[is.na(vec4)] = 4

mean(vec4)

## [1] 4

Eduardo Martínez Introduction to R 2022-09-30 45 / 121


Truncated data

vec6 > 40 #find

## [1] TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE
## [13] FALSE FALSE FALSE FALSE
vec6[vec6 > 40] #filter

## [1] 50 48 46 44 42
vec6[vec6 > 40] = 40 #replace

Eduardo Martínez Introduction to R 2022-09-30 46 / 121


Delete elements of a vector

vec5

## [1] 2 4 6 8 10 12 14 16
vec5[-2]

## [1] 2 6 8 10 12 14 16
vec5 = vec5[-2]

Eduardo Martínez Introduction to R 2022-09-30 47 / 121


Delete elements of a vector

vec5[-(3:5)] # from 3rd to 5th

## [1] 2 6 14 16
vec5[-c(3,5)] # 3rd and 5th

## [1] 2 6 10 14 16
vec5[c(3,5)] = c(-100,100)

Eduardo Martínez Introduction to R 2022-09-30 48 / 121


Delete elements of a vector

vec5[vec5 > 10] = 8

vec5

## [1] 2 6 -100 10 8 8 8
vec4[vec4 != 4]

## [1] 3 3 8 2

Eduardo Martínez Introduction to R 2022-09-30 49 / 121


Fixing vec3

class(vec3)

## [1] "character"
as.numeric(vec3)

## Warning: NAs introduced by coercion


## [1] 3 NA 4 NA 3 8 2

Eduardo Martínez Introduction to R 2022-09-30 50 / 121


Fixing vec3

vec3 = as.numeric(vec3)

## Warning: NAs introduced by coercion


vec3[2] = 1
vec3[4] = 2
sum(vec3)

## [1] 23

Eduardo Martínez Introduction to R 2022-09-30 51 / 121


Coercion

var1 = "3"
var2 = "4"
# var1 + var2

Eduardo Martínez Introduction to R 2022-09-30 52 / 121


Coercion

as.numeric(var1) + as.numeric(var2)

## [1] 7
# as.logical
# as.character
# as.complex (rarely used)

Eduardo Martínez Introduction to R 2022-09-30 53 / 121


Matrices

m1 = c(1,2,4)
m2 = c(3,2,5)
m = matrix(c(m1,m2), nrow = 3, ncol =2) # Change nrow and nco
m

## [,1] [,2]
## [1,] 1 3
## [2,] 2 2
## [3,] 4 5

Eduardo Martínez Introduction to R 2022-09-30 54 / 121


Matrices

m3 = c(1,5,6)

m= matrix(c(m1,m2,m3), nrow = 3, ncol = 3) # opción 1: usar ma

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


## [1,] 1 3 1
## [2,] 2 2 5
## [3,] 4 5 6

Eduardo Martínez Introduction to R 2022-09-30 55 / 121


Matrices

m = matrix(c(m1,m2), nrow = 3, ncol =2) # opci?n 2: cbind


m

## [,1] [,2]
## [1,] 1 3
## [2,] 2 2
## [3,] 4 5
m5 = cbind(m,m3) #column binding, tambi?n existe rbind
m5

## m3
## [1,] 1 3 1
## [2,] 2 2 5
## [3,] 4 5 6

Eduardo Martínez Introduction to R 2022-09-30 56 / 121


Matrices

Wrong use of rbind rbind(m,m3)

Eduardo Martínez Introduction to R 2022-09-30 57 / 121


Matrices

# fixing the previous rbind sentence


rbind(m5,m3)

## m3
## 1 3 1
## 2 2 5
## 4 5 6
## m3 1 5 6

Eduardo Martínez Introduction to R 2022-09-30 58 / 121


Slicing a matrix with [ row, col ]
m5

## m3
## [1,] 1 3 1
## [2,] 2 2 5
## [3,] 4 5 6
m5[1:2, ] #row 1 a 2, all columns

## m3
## [1,] 1 3 1
## [2,] 2 2 5
m5[,1:2] #col 1 a 2, all rows

##
## [1,] 1 3
## [2,] 2 2
## [3,] 4 5
Eduardo Martínez Introduction to R 2022-09-30 59 / 121
Usign c( ) to “jump”
m5

## m3
## [1,] 1 3 1
## [2,] 2 2 5
## [3,] 4 5 6
m5[c(1,3),]

## m3
## [1,] 1 3 1
## [2,] 4 5 6
m5[,c(1,3)]

## m3
## [1,] 1 1
## [2,] 2 5
## [3,] 4 6
Eduardo Martínez Introduction to R 2022-09-30 60 / 121
Linear algebra functions

m5

## m3
## [1,] 1 3 1
## [2,] 2 2 5
## [3,] 4 5 6
diag(m5)

## [1] 1 2 6
t(m5)

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


## 1 2 4
## 3 2 5
## m3 1 5 6

Eduardo Martínez Introduction to R 2022-09-30 61 / 121


Linear algebra functions

det(m5)

## [1] 13
dim(m)

## [1] 3 2

Eduardo Martínez Introduction to R 2022-09-30 62 / 121


Operations

m6 = t(m5)
m5 + m6

## m3
## [1,] 2 5 5
## [2,] 5 4 10
## [3,] 5 10 12
m5 + 3 ## recursion

## m3
## [1,] 4 6 4
## [2,] 5 5 8
## [3,] 7 8 9

Eduardo Martínez Introduction to R 2022-09-30 63 / 121


Operations

m5 + matrix(3, nrow=3, ncol=3)

## m3
## [1,] 4 6 4
## [2,] 5 5 8
## [3,] 7 8 9
m5 + 3 == m5 + matrix(3, nrow=3, ncol=3)

## m3
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE

Eduardo Martínez Introduction to R 2022-09-30 64 / 121


Operations

m5 %*% m6

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


## [1,] 11 13 25
## [2,] 13 33 48
## [3,] 25 48 77
m5 * m6

## m3
## [1,] 1 6 4
## [2,] 6 4 25
## [3,] 4 25 36

Eduardo Martínez Introduction to R 2022-09-30 65 / 121


Vectorization over a matrix

exp(m)

## [,1] [,2]
## [1,] 2.718282 20.085537
## [2,] 7.389056 7.389056
## [3,] 54.598150 148.413159
sqrt(m)

## [,1] [,2]
## [1,] 1.000000 1.732051
## [2,] 1.414214 1.414214
## [3,] 2.000000 2.236068

Eduardo Martínez Introduction to R 2022-09-30 66 / 121


Matrix from a sequence

m7 = matrix(1:12,nrow = 4, ncol = 3)

m8 = rbind(m5,m6,m7)

Eduardo Martínez Introduction to R 2022-09-30 67 / 121


Section 3

Control Structures

Eduardo Martínez Introduction to R 2022-09-30 68 / 121


Context

Problem: Determine whether a number is “big” or not Solution: Compare


with 30
test = 300

Eduardo Martínez Introduction to R 2022-09-30 69 / 121


if then

if(test < 30){


print('small')
}

Eduardo Martínez Introduction to R 2022-09-30 70 / 121


if then else

if(test < 30){


print('small')
}else{
print('big')
}

## [1] "big"

Eduardo Martínez Introduction to R 2022-09-30 71 / 121


if anidados

if(test < 30){


print('es un número pequeño')
}else{if(test>30){
print('es un número grande')
}else{
print('the edge')
}
}

## [1] "es un número grande"

Eduardo Martínez Introduction to R 2022-09-30 72 / 121


Vectorized if (similar to Excel)

ifelse(test < 30, 'small', 'big')

## [1] "big"

Eduardo Martínez Introduction to R 2022-09-30 73 / 121


Control structures and vectorization

if(vec6 < 30){ print(‘small’) }else{ print(‘big’) }

Eduardo Martínez Introduction to R 2022-09-30 74 / 121


Control structures and vectorization

ifelse(vec6 < 30, 'small', 'big')

## [1] "big" "big" "big" "big" "big" "big" "big"


## [10] "big" "big" "small" "small" "small" "small" "small
ifelse(vec1 %% 2 == 0, 'even','odd')

## [1] "even" "even" "odd" "even" "odd" "even" "odd"

Eduardo Martínez Introduction to R 2022-09-30 75 / 121


For loop

list = vec1

# variable i as a counter

for(i in 1:7){
print(list[i])
}

## [1] 8
## [1] 6
## [1] 7
## [1] 6
## [1] 3
## [1] 0
## [1] 9

Eduardo Martínez Introduction to R 2022-09-30 76 / 121


Standarization

for(i in 1:7){
print((list[i] - mean(list))/sd(list))
}

## [1] 0.7830416
## [1] 0.1381838
## [1] 0.4606127
## [1] 0.1381838
## [1] -0.8291029
## [1] -1.79639
## [1] 1.105471

Eduardo Martínez Introduction to R 2022-09-30 77 / 121


Storing

standardlist = c()
for(i in 1:7){
standardlist[i]=(list[i] - mean(list))/sd(list)
}
standardlist

## [1] 0.7830416 0.1381838 0.4606127 0.1381838 -0.8291029

Eduardo Martínez Introduction to R 2022-09-30 78 / 121


Fixing the end of the loop

for(i in 1:length(list)){
print(list[i])
}

## [1] 8
## [1] 6
## [1] 7
## [1] 6
## [1] 3
## [1] 0
## [1] 9

Eduardo Martínez Introduction to R 2022-09-30 79 / 121


Jumps

for(i in seq(1,7,by=2)){
print(list[i])
}

## [1] 8
## [1] 7
## [1] 3
## [1] 9

Eduardo Martínez Introduction to R 2022-09-30 80 / 121


for + if works like vectotrization

for(i in 1:length(list)){
if(list[i] < 30){
print('small')
}else{
print('big')
}
}

## [1] "small"
## [1] "small"
## [1] "small"
## [1] "small"
## [1] "small"
## [1] "small"
## [1] "small"

Eduardo Martínez Introduction to R 2022-09-30 81 / 121


for runs over lists

for(i in list){
print(i)
}

## [1] 8
## [1] 6
## [1] 7
## [1] 6
## [1] 3
## [1] 0
## [1] 9

Eduardo Martínez Introduction to R 2022-09-30 82 / 121


for and validations

for(value in list){
if(value>5){
print("not valid")
}
}

## [1] "not valid"


## [1] "not valid"
## [1] "not valid"
## [1] "not valid"
## [1] "not valid"

Eduardo Martínez Introduction to R 2022-09-30 83 / 121


for and validations

for(value in list){
if(value>5){
print(paste(value," is not valid"))
}
}

## [1] "8 is not valid"


## [1] "6 is not valid"
## [1] "7 is not valid"
## [1] "6 is not valid"
## [1] "9 is not valid"

Eduardo Martínez Introduction to R 2022-09-30 84 / 121


for and validations

for(i in 1:length(list)){
if(list[i] > 5){
print(paste("The value at position ",i,"is not valid becau
}
}

## [1] "The value at position 1 is not valid because 8 is g


## [1] "The value at position 2 is not valid because 6 is g
## [1] "The value at position 3 is not valid because 7 is g
## [1] "The value at position 4 is not valid because 6 is g
## [1] "The value at position 7 is not valid because 9 is g

Eduardo Martínez Introduction to R 2022-09-30 85 / 121


While loop

i = 1
while(i <= length(list)){
print(list[i])
i=i+1
}

## [1] 8
## [1] 6
## [1] 7
## [1] 6
## [1] 3
## [1] 0
## [1] 9

Eduardo Martínez Introduction to R 2022-09-30 86 / 121


Section 4

Functions

Eduardo Martínez Introduction to R 2022-09-30 87 / 121


Defining functions

isbig <- function(n){


if(n < 30){
print('small')
}else{if(n>30){
print('big')
}else{
print('IDK')
}

}
}

Eduardo Martínez Introduction to R 2022-09-30 88 / 121


Calling a function

isbig(15)

## [1] "small"
isbig(150)

## [1] "big"
isbig(30)

## [1] "IDK"

Eduardo Martínez Introduction to R 2022-09-30 89 / 121


Even function

iseven = function(n){
remainder = n %% 2
if(remainder == 0)
return(TRUE)
return(FALSE)
}

Eduardo Martínez Introduction to R 2022-09-30 90 / 121


Even function

iseven(15)

## [1] FALSE
iseven(258928)

## [1] TRUE
# iseven(vec1) # Wrong

sapply(vec1, iseven) # vectorized function

## [1] TRUE TRUE FALSE TRUE FALSE TRUE FALSE

Eduardo Martínez Introduction to R 2022-09-30 91 / 121


Function with seevral arguments

isdivisible <- function(N1,N2){


if(N1 %% N2 == 0)
return(TRUE)
return(FALSE)
}

isdivisible(15,4)

## [1] FALSE
isdivisible(21616431684,4)

## [1] TRUE

Eduardo Martínez Introduction to R 2022-09-30 92 / 121


if in a function

isdivisible2 <- function(N1,N2){


ifelse(N1 %% N2 == 0,return('yes'),return('no'))
}

isdivisible2(15,4)

## [1] "no"

Eduardo Martínez Introduction to R 2022-09-30 93 / 121


Function over a list

overaverage = function(list,value){
if(value > mean(list))
return('above average')
return('not above average')
}

overaverage(vec6,40)

## [1] "above average"


overaverage(vec1,7)

## [1] "above average"

Eduardo Martínez Introduction to R 2022-09-30 94 / 121


Calling functions
for(value in vec5){
print(value)
print(overaverage(vec5, value))
}

## [1] 2
## [1] "above average"
## [1] 6
## [1] "above average"
## [1] -100
## [1] "not above average"
## [1] 10
## [1] "above average"
## [1] 8
## [1] "above average"
## [1] 8
## [1] "above average"
## [1] 8 Martínez
Eduardo Introduction to R 2022-09-30 95 / 121
Section 5

DataFrame

Eduardo Martínez Introduction to R 2022-09-30 96 / 121


Loading data

favs <- read.table("favorites.txt", sep="," , header = TRUE)

Eduardo Martínez Introduction to R 2022-09-30 97 / 121


Using dialog frame

# favs <- read.table(file.choose(), sep="," , header = TRUE)

Eduardo Martínez Introduction to R 2022-09-30 98 / 121


read.csv

favs = read.csv("favorites.txt")

Eduardo Martínez Introduction to R 2022-09-30 99 / 121


Class

class(favs)

## [1] "data.frame"
head(favs) ## first 5

## flavor number
## 1 pistachio 6
## 2 mint chocolate chip 7
## 3 vanilla 5
## 4 chocolate 10
## 5 strawberry 2
## 6 neopolitan 4

Eduardo Martínez Introduction to R 2022-09-30 100 / 121


Use of $ to choose a column

names(favs)

## [1] "flavor" "number"


class(favs$flavor)

## [1] "character"
class(favs$number)

## [1] "integer"

Eduardo Martínez Introduction to R 2022-09-30 101 / 121


String as factor

favs = read.csv("favorites.txt", stringsAsFactors = TRUE)


class(favs$flavor)

## [1] "factor"

Eduardo Martínez Introduction to R 2022-09-30 102 / 121


Using [ ]

favs$flavor

## [1] pistachio mint chocolate chip vanilla


## [4] chocolate strawberry neopolitan
## 6 Levels: chocolate mint chocolate chip neopolitan pistachi
favs[["flavor"]]

## [1] pistachio mint chocolate chip vanilla


## [4] chocolate strawberry neopolitan
## 6 Levels: chocolate mint chocolate chip neopolitan pistachi

Eduardo Martínez Introduction to R 2022-09-30 103 / 121


Using [ ]

favs[,1]

## [1] pistachio mint chocolate chip vanilla


## [4] chocolate strawberry neopolitan
## 6 Levels: chocolate mint chocolate chip neopolitan pistachi
favs[1:3,]

## flavor number
## 1 pistachio 6
## 2 mint chocolate chip 7
## 3 vanilla 5

Eduardo Martínez Introduction to R 2022-09-30 104 / 121


Rename a column

names(favs)

## [1] "flavor" "number"


names(favs)[1] = "flav"

names(favs)

## [1] "flav" "number"

Eduardo Martínez Introduction to R 2022-09-30 105 / 121


Structure

str(favs)

## 'data.frame': 6 obs. of 2 variables:


## $ flav : Factor w/ 6 levels "chocolate","mint chocolate c
## $ number: int 6 7 5 10 2 4

Eduardo Martínez Introduction to R 2022-09-30 106 / 121


Section 6

Graphs - ggplot2

Eduardo Martínez Introduction to R 2022-09-30 107 / 121


install.packages("ggplot2")

## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-lib


## (as 'lib' is unspecified)
library(ggplot2)

Eduardo Martínez Introduction to R 2022-09-30 108 / 121


ggplot(favs, aes(x=flav,y=number))+ggtitle("Ice Cream") + geom
Ice Cream

10.0

7.5
number

5.0

2.5

0.0

chocolate mint chocolate chip neopolitan pistachio strawberry vanilla


flav
Eduardo Martínez Introduction to R 2022-09-30 109 / 121
mtcars
mtcars

## mpg cyl disp hp drat wt qsec vs


## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0
## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0
## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1
## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0
## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1
## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1
## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1
## Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1
## Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0
## Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0
## Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0
## Cadillac Fleetwood
Eduardo Martínez 10.4 8 to472.0
Introduction R 205 2.93 5.250 17.98
2022-09-30 0
110 / 121
Frequency table

unique(mtcars$carb)

## [1] 4 1 2 3 6 8
table(mtcars$carb)

##
## 1 2 3 4 6 8
## 7 10 3 10 1 1

Eduardo Martínez Introduction to R 2022-09-30 111 / 121


Bar graph
qplot(factor(carb),data = mtcars, geom = "bar")

## Warning: `qplot()` was deprecated in ggplot2 3.4.0.


## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where th
## generated.

10.0

7.5

5.0

Eduardo Martínez Introduction to R 2022-09-30 112 / 121


Bar graph
qplot(factor(carb),data = mtcars, geom = "bar",xlab="number of

10.0

7.5

5.0

2.5

Eduardo Martínez Introduction to R 2022-09-30 113 / 121


Bar graphs
qplot(factor(carb),data = mtcars,
geom = "bar",xlab="number of carburetors",
fill =factor(carb))

10.0

7.5

factor(carb)
1
2

5.0 3
4
6
8

2.5

Eduardo Martínez Introduction to R 2022-09-30 114 / 121


Intervals

cut(mtcars$mpg, 6)

## [1] (18.2,22.1] (18.2,22.1] (22.1,26.1] (18.2,22.1] (18.2,


## [7] (10.4,14.3] (22.1,26.1] (22.1,26.1] (18.2,22.1] (14.3,
## [13] (14.3,18.2] (14.3,18.2] (10.4,14.3] (10.4,14.3] (14.3,
## [19] (30,33.9] (30,33.9] (18.2,22.1] (14.3,18.2] (14.3,
## [25] (18.2,22.1] (26.1,30] (22.1,26.1] (30,33.9] (14.3,
## [31] (14.3,18.2] (18.2,22.1]
## 6 Levels: (10.4,14.3] (14.3,18.2] (18.2,22.1] (22.1,26.1] .
freq = table(cut(mtcars$mpg, 6))

Eduardo Martínez Introduction to R 2022-09-30 115 / 121


Histogram
qplot(mpg, data=mtcars, geom="histogram")

## `stat_bin()` using `bins = 30`. Pick better value with `bin

Eduardo Martínez Introduction to R 2022-09-30 116 / 121


Histogram
qplot(mpg, data=mtcars, geom="histogram", binwidth = 3.9)

10.0

7.5

5.0

2.5

Eduardo Martínez Introduction to R 2022-09-30 117 / 121


Histogram
qplot(mpg, data=mtcars, geom="histogram", binwidth = 3.9, colo

10.0

7.5

5.0

2.5

Eduardo Martínez Introduction to R 2022-09-30 118 / 121


KDE
qplot(mpg, data=mtcars, geom="density")

0.06

0.04

0.02

Eduardo Martínez Introduction to R 2022-09-30 119 / 121


Bonus

## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-lib


## (as 'lib' is unspecified)
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-lib
## (as 'lib' is unspecified)
## Using libcurl 7.68.0 with OpenSSL/1.1.1f

Eduardo Martínez Introduction to R 2022-09-30 120 / 121


Import JSON
df = fromJSON('https://www.datos.gov.co/resource/24ny-2dhf.jso
head(df)

## icount entidad pr latitud longitud clasificacion


## 1 2 INVIAS-OTROS 37 1.020441 -77.698917 Muy Bajo
## 2 2 ANI 36 0.997095 -77.474728 Muy Bajo
## 3 4 ANI 79 1.163092 -77.286501 Muy Bajo
## 4 2 INVIAS-OTROS sd 1.168242 -77.255688 Muy Bajo
## 5 2 INVIAS-OTROS 20 3.879461 -76.922322 Muy Bajo
## 6 2 INVIAS-OTROS 26 3.870278 -76.87666 Muy Bajo
## departamento divipola id_mt id_tramo ti
## 1 NARIÑO 52720 <NA> <NA>
## 2 NARIÑO 52352 4G025 RumPast_Tramo_UF3
## 3 NARIÑO 52001 4G025 RumPast_Tramo_UF5
## 4 NARIÑO 52001 <NA> RPC_Tramo_5VOrientPasto S
## 5 VALLE DEL CAUCA 76109 <NA> ViaPuerto_Tramo_UF0 S
## 6 VALLE DEL CAUCA 76109 <NA> ViaPuerto_Tramo_UF0 S
Eduardo Martínez Introduction to R 2022-09-30 121 / 121

You might also like