Useful Functions

Intermediate R

Filip Schouwenaars

DataCamp Instructor

Loads of useful functions

  • sapply(), vapply(), lapply()
  • sort()
  • print()
  • identical() ...
Intermediate R

Mathematical utilities

v1 <- c(1.1, -7.1, 5.4, -2.7)
v2 <- c(-3.6, 4.1, 5.8, -8.0)
mean(c(sum(round(abs(v1))), sum(round(abs(v2)))))
Intermediate R

abs()

v1 <- c(1.1, -7.1, 5.4, -2.7)
v2 <- c(-3.6, 4.1, 5.8, -8.0)
mean(c(sum(round(abs(v1))), sum(round(abs(v2)))))
abs(c(1.1, -7.1, 5.4, -2.7))
1.1 7.1 5.4 2.7
abs(c(-3.6, 4.1, 5.8, -8.0))
3.6 4.1 5.8 8.0
mean(c(sum(round(c(1.1, 7.1, 5.4, 2.7))), 
       sum(round(c(3.6, 4.1, 5.8, 8.0)))))
Intermediate R

round()

v1 <- c(1.1, -7.1, 5.4, -2.7)
v2 <- c(-3.6, 4.1, 5.8, -8.0)
mean(c(sum(round(abs(v1))), sum(round(abs(v2)))))
mean(c(sum(round(c(1.1, 7.1, 5.4, 2.7))), 
       sum(round(c(3.6, 4.1, 5.8, 8.0)))))
round(c(1.1, 7.1, 5.4, 2.7))
1 7 5 3
round(c(3.6, 4.1, 5.8, 8.0))
4 4 6 8
Intermediate R

sum()

v1 <- c(1.1, -7.1, 5.4, -2.7)
v2 <- c(-3.6, 4.1, 5.8, -8.0)
mean(c(sum(round(abs(v1))), sum(round(abs(v2)))))
mean(c(sum(c(1, 7, 5, 3)),
       sum(c(4, 4, 6, 8))))
sum(c(1, 7, 5, 3))
16
sum(c(4, 4, 6, 8))
22
Intermediate R

mean()

mean(c(16, 22))
19
v1 <- c(1.1, -7.1, 5.4, -2.7)
v2 <- c(-3.6, 4.1, 5.8, -8.0)
mean(c(sum(round(abs(v1))), sum(round(abs(v2)))))
19
Intermediate R

Functions for data structures

li <- list(log = TRUE, 
           ch = "hello", 
           int_vec = sort(rep(seq(8, 2, by = -2), times = 2)))
sort(rep(seq(8, 2, by = -2), times = 2)))
Intermediate R

seq()

li <- list(log = TRUE, 
           ch = "hello", 
           int_vec = sort(rep(seq(8, 2, by = -2), times = 2)))
sort(rep(seq(8, 2, by = -2), times = 2)))
seq(1, 10, by = 3)
1  4  7 10
seq(8, 2, by = -2)
8 6 4 2
Intermediate R

rep()

li <- list(log = TRUE, 
           ch = "hello", 
           int_vec = sort(rep(seq(8, 2, by = -2), times = 2)))
sort(rep(c(8, 6, 4, 2), times = 2))
rep(c(8, 6, 4, 2), times = 2)
8 6 4 2 8 6 4 2
rep(c(8, 6, 4, 2), each = 2)
8 8 6 6 4 4 2 2
Intermediate R

sort()

li <- list(log = TRUE, 
           ch = "hello", 
           int_vec = sort(rep(seq(8, 2, by = -2), times = 2)))
sort(c(8, 6, 4, 2, 8, 6, 4, 2))
2 2 4 4 6 6 8 8
sort(c(8, 6, 4, 2, 8, 6, 4, 2), decreasing = TRUE)
8 8 6 6 4 4 2 2
Intermediate R

sort()

sort(rep(seq(8, 2, by = -2), times = 2))
2 2 4 4 6 6 8 8
Intermediate R

str()

li <- list(log = TRUE, 
           ch = "hello", 
           int_vec = sort(rep(seq(8, 2, by = -2), times = 2)))
str(li)
List of 3
 $ log    : logi TRUE
 $ ch     : chr "hello"
 $ int_vec: num [1:8] 2 2 4 4 6 6 8 8
Intermediate R

is.*(), as.*()

is.list(li)
TRUE
is.list(c(1, 2, 3))
FALSE
li2 <- as.list(c(1, 2, 3))
is.list(li2)
TRUE
Intermediate R

is.*(), as.*()

unlist(li)
     log       ch  int_vec1  int_vec2  ... int_vec7  int_vec8 
  "TRUE"  "hello"       "2"       "2"  ...      "8"       "8" 
Intermediate R

append(), rev()

str(append(li, rev(li)))
str(rev(li))
List of 3
 $ int_vec: num [1:8] 2 2 4 4 6 6 8 8
 $ ch     : chr "hello"
 $ log    : logi TRUE
Intermediate R

append(), rev()

str(append(li, rev(li)))
List of 3
 $ int_vec: num [1:8] 2 2 4 4 6 6 8 8
 $ ch     : chr "hello"
 $ log    : logi TRUEstr(append(li, rev(li)))
List of 6
 $ log    : logi TRUE
 $ ch     : chr "hello"
 $ int_vec: num [1:8] 2 2 4 4 6 6 8 8
 $ int_vec: num [1:8] 2 2 4 4 6 6 8 8
 $ ch     : chr "hello"
 $ log    : logi TRUE
Intermediate R

Let's practice!

Intermediate R

Preparing Video For Download...