R Basics

Updated: 03 September 2023

Based on this EdX Course

Configuration

Before starting the course make sure you install the library with the relevant datasets included

1
install.packages("dslabs")
2
library(dslabs)

From the dslabs library you can use the data sets as needed

Objects

In order to store a value as a variable we use the assignment variable

1
a <- 25

To display this object we can use

1
print(a)

Functions

A Data Analysis process is typically a series of functions applied to data

We can define a function in R using:

1
myfunction <- function(arg1, arg2, ... ){
2
statements
3
return(object)
4
}

To Evaluate a function we use the parenthesis and arguments:

1
log(a)

We can nest function calls inside of function arguments, for example:

1
log((exp(a)))

To get help for a function we can use the following:

1
?log
2
help("log")

We can enter function arguments in an order different to the default by using named arguments:

1
log(x=5, base=3)

Otherwise the arguments are evaluated in order

Comments

Commenting in R is done with the # symbol:

1
# This is a comment

Data Types

R makes use of different data types, in R we typically use DataFrames to store data, these can store a mixture of different data types in a collection

To make use of DataFrames we need to import the dslabs library:

1
library(dslabs)

To check the type of an object we an use

1
class(object)

In order to view the structure of an object we can use

1
str(object)

If we want to view the data in a DataFrame, we can use:

1
head(dataFrame)

to Access a variable in an object we use the $ accessesor, this preserves the rows in the DataFrame

data$names will list the names column of the DataFrame

In R we refer to the data points in our DataFrame or Matrix as Vectors

We can use the == as the logical operator

Factors allow us to store catergorical data, we can view the different catergories with the following:

1
> class(dataFrame$gender)
2
[1] Factor
3
> levels(dataFrame$gender)
4
[2] "Male" "Female"

Vectors

The most basic data unit in R is a Vector

To create a vector we can use the concatonate function with:

1
codes <- c(380, 124, 818)

If we want to name the values we can do so as follows:

1
codes <- c(italy=380, canada=124, egypt=818)
2
codes <- c("italy"=380, "canada"=124, "egypt"=818)

Getting a sequence of number we can use:

1
> seq(1, 5)
2
[1] 1, 2, 3, 4, 5
3
4
> seq(1, 10, 2)
5
[1] 1, 3, 5, 7, 9

We can access an element of a vector with either a single access or multi-access vector as follows:

1
> codes[3]
2
[1] 818
3
4
> codes["canada"]
5
[2] 124
6
7
> codes["canada", "egypt"]
8
[3] 124 818
9
10
> codes[1:2]
11
[4] 380 124

Vector Coercion

Coercion is an attempt by R to guess the type of a variable if it’s of a different type of the rest of the values

1
x <- c(1, "hello", 3)
2
[1] "1" "hello" "3"

If we want to force a coercion we can use the as.character function or as.numeric function as follows:

1
> x <- 1:5
2
> y <- as.character(1:5)
3
> y
4
[1] "1" "2" "3" "4" "5"
5
> as.numeric(y)
6
[2] 1 2 3 4 5

If R is unable to coerse a value it will result in NA which is very common with data sets as it refers to missing data

Sorting

The sort function will sort a vector in increasing order, however this gives us no relation to the positions of that data. We can use the order function to reuturn the index of the values that are sorted

1
> x
2
[1] 31 4 15 92 65
3
4
> sort(x)
5
[2] 4 15 31 65 92
6
7
> order(x)
8
[3] 2 3 1 5 4

The entries of vectors that the vectors are ordered by correspond to their rows in the DataFrame, therefore we can order one row by another

1
index <- order(data.total)
2
data$name[index]

To get the max or min value we can use:

1
max(data$total) # maximum value
2
which.max(data$total) # index of maximum value
3
4
min(data$total) # minimum value
5
which.min(data$total) # index of minimum value

The rank function will return the index of the sizes of the vectors

Vector Aritmetic

Aritmetic operations occur element-wise

If we operate with a single value the operation will work per element, however if we do this with two vectors, we will add it element-wise, v3 <- v1 + v2 will mean v3[1] <- v1[1] + v2[1] and so on

Indexing

R provides ways to index vectors based on properties on another vector, this allows us to make use of logical comparators, etc.

1
> large_tots <- data$total > 200
2
[1] TRUE TRUE FALSE TRUE FALSE
3
4
> small_size <- data$size < 20
5
[2] FALSE TRUE TRUE TRUE FALSE
6
7
index <- large_tots && small_size
8
[3] FALSE TRUE FALSE TRUE FALSE

Indexing Functinos

  • which will give us the indexes which are true which(data$total > 200) this will only return the values that are true
  • match returns the values in one vector where another occurs match(c(20, 14, 5), data$size) will return only the values in which data$size == 20 || 14 || 5
  • %in% if we want to check if the contents of a vector are in another vector, for example:
1
> x <- c("a", "b", "c", "d", "e")
2
> y <- c("a", "d", "f")
3
> y %in% x
4
[1] TRUE, TRUE, FALSE

These functions are very useful for subsetting datasets

Data Wrangling

The dplyr package is useful for manipulating tables of data

  • Add or change a column with mutate
  • Filter data by rows with filter
  • Filter data by columns with select
1
mutate(data, rate=total/size) # Add rate column based on two other columns
2
3
select(data, name, rate) # Will create a new table with only the name and rate columns
4
5
filter(data, rate <= 0.7) # Will filter out the rows where the rate expression is true

We can combine functions using the pipe operator:

1
dataTable %>% select(name, rate) %>% filter(rate <= 0.7)

Creating Data Frames

we can create a data frame with the data.frame function as follows:

1
data <- data.frame(names = c("John","James", "Jenny"),
2
exam_1 = c(90, 29, 45),
3
exam_2 = c(30, 10, 95))

Howewever, by default R will pass strings as Factors, to prevent this we use the stringsAsFactors argument:

1
data <- data.frame(names = c("John","James", "Jenny"),
2
exam_1 = c(90, 29, 45),
3
exam_2 = c(30, 10, 95),
4
stringsAsFactors = FALSE)

Basic Plots

We can make simple plots very easily with the following functions:

  • plot(dataFrame$size, data$rate)
  • lines(dataFrame$size, data$rate)
  • hist(dataFrame$size)
  • boxplot(rate~catergory, data=dataFrame)

Programming Basics

Conditionals

1
# Can evalutate all elements of a vector
2
if (test_expression) {
3
statement1
4
} else {
5
statement2
6
}
7
8
# Will reuturn a result
9
ifelse(comparison, trueReturn, falseReturn)
10
11
# Will return true if any value in vector meets condition
12
any(condition)
13
14
# Will return true if all values meet condition
15
all(condition)

Functions

Functions in R are objects, if we need to write a function in R we can do this wth the following:

1
myfunction <- function(arg1, arg2, optional=TRUE ){
2
statements
3
return(object)
4
}

This will make use of the usual lexical scoping

For Loops

1
for (i in sequence) {
2
statements
3
}

At the end of our loop the index value will hold it’s last value

Other Functions

In R we rarely use for-loops We can use other functions like the following:

  • apply
  • sapply
  • tapply
  • mapply

Other functions that are widely used are:

  • split
  • cut
  • quantile
  • reduce
  • identical
  • unique