Intermediate R

Equality

1
2
3
4
5
6
== / identical()
!=
< and > # greater and less than
& # and
| # or
! # reverse the result

Compare

Compare vectors

linkedin > facebook

Compare matrices

views <= 14

The if statement

1
2
3
4
5
6
7
8
9
if (condition) {
expr
} else if (condition2) {
expr2
} else if (condition3) {
expr3
} else {
expr4
}

Write a while loop

1
2
3
while (condition) {
expr
}

Stop the while loop:

1
break

Loop over a vector

1
primes <- c(2, 3, 5, 7, 11, 13)

loop version 1

1
2
3
for (p in primes) {
print(p)
}

loop version 2

1
2
3
for (i in 1:length(primes)) {
print(primes[i])
}

Loop over a list

1
primes_list <- list(2, 3, 5, 7, 11, 13)

loop version 1

1
2
3
for (p in primes_list) {
print(p)
}

loop version 2

1
2
3
for (i in 1:length(primes_list)) {
print(primes_list[[i]])
}

Loop over a matrix

1
2
3
4
5
for (var1 in seq1) {
for (var2 in seq2) {
expr
}
}
1
strsplit()

Function documentation

1
2
3
help(sample)
?sample
args(sample)

Write your own function

1
2
3
4
my_fun <- function(arg1, arg2=TRUE) {
body
return(result)
}

Load an R Package

• install.packages(), which as you can expect, installs a given package.
• library() which loads packages, i.e. attaches them to the search list on your R workspace. / require()
• search(), to look at the currently attached packages and
• qplot(mtcars$wt, mtcars$hp), to build a plot of two variables of the mtcars data frame.

1
library(ggplot2)

a powerful package for data visualization

Use lapply with a built-in R function

1
lapply(X, FUN, ...)

To put it generally, lapply takes a vector or list X, and applies the function FUN to each of its members.

To put it generally, lapply takes a vector or list X, and applies the function FUN to each of its members.

lapply and anonymous functions

Named function

1
triple <- function(x) { 3 * x }

Anonymous function with same implementation

1
function(x) { 3 * x }

Use anonymous function inside lapply()

1
lapply(list(1,2,3), function(x) { 3 * x })

Use lapply with additional arguments

1
2
3
4
multiply <- function(x, factor) {
x * factor
}
lapply(list(1,2,3), multiply, factor = 3)

How to use sapply

1
sapply(X, FUN, ...)

sapply() simplifies the list that lapply() would return by turning it into a vector.

sapply() simplifies the list that lapply() would return by turning it into a vector.

Use vapply

1
vapply(X, FUN, FUN.VALUE, ..., USE.NAMES = TRUE)

Over the elements inside X, the function FUN is applied. The FUN.VALUE argument expects a template for the return argument of this function FUN. USE.NAMES is TRUE by default; in this case vapply() tries to generate a named array, if possible.

Data Utilities

R features a bunch of functions to juggle around with data structures::
• seq(): Generate sequences, by specifying the from, to, and by arguments.
• rep(): Replicate elements of vectors and lists.
• sort(): Sort a vector in ascending order. Works on numerics, but also on character strings and logicals.
• rev(): Reverse the elements in a data structures for which reversal is defined.
• str(): Display the structure of any R object.
• append(): Merge vectors or lists.
• is.(): Check for the class of an R object.
• as.
(): Convert an R object from one class to another.
• unlist(): Flatten (possibly embedded) lists to produce a vector.

1
grepl & grep

In their most basic form, regular expressions can be used to see whether a pattern exists inside a character string or a vector of character strings. For this purpose, you can use:
• grepl(), which returns TRUE when a pattern is found in the corresponding character string.
• grep(), which returns a vector of indices of the character strings that contains the pattern.

You can use the caret, ^, and the dollar sign, $ to match the content located in the start and end of a string, respectively. This could take us one step closer to a correct pattern for matching only the “.edu” email addresses from our list of emails. But there’s more that can be added to make the pattern more robust:
• @, because a valid email must contain an at-sign.
• ., which matches any character (.) zero or more times (). Both the dot and the asterisk are metacharacters. You can use them to match any character between the at-sign and the “.edu” portion of an email address.
• \.edu​$, to match the “.edu” part of the email at the end of the string. The \\ part escapes the dot: it tells R that you want to use the . as an actual character.

While grep() and grepl() were used to simply check whether a regular expression could be matched with a character vector, sub() and gsub() take it one step further: you can specify a replacement argument. If inside the character vector x, the regular expression pattern is found, the matching element(s) will be replaced with replacement.sub() only replaces the first match, whereas gsub() replaces all matches.

Create and format dates
To create a Date object from a simple character string in R, you can use the as.Date() function. The character string has to obey a format that can be defined using a set of symbols (the examples correspond to 13 January, 1982):
• %Y: 4-digit year (1982)
• %y: 2-digit year (82)
• %m: 2-digit month (01)
• %d: 2-digit day of the month (13)
• %A: weekday (Wednesday)
• %a: abbreviated weekday (Wed)
• %B: month (January)
• %b: abbreviated month (Jan)