Exercises: solutions: Introduction
Google is your best friend:
max(1,20,15,13,17,18,12,5)
2.3.1 Som van integers 1,…,100
100*(100+1)/2
2.3.2 Som van integers 1,…,1000
1000*(1000+1)/2
2.3.3 Interpreteer code
2
Functions
x <- c(5,6,1,2)
Geneste functies
log10(sqrt(100))
Variables in a dataframe
2
Variables names
# Load package and data
library(ISLR2)
data(Hitters)
names(Hitters)
# Use the function names to extract the variable names
Examining variables
# Load package and data
library(ISLR2)
data(Hitters)
# Use the accessor to extract Hits and assign it to the object a
a <- Hitters$Hits
# Determine the class of a
class(a)
Multiple ways to access variables
, # Load package and data
library(ISLR2)
data(Hitters)
# Use the accessor to extract Hits and assign it to a
a <- Hitters$Hits
# Use the square brackets to extract Hits and assign it to b
b <- Hitters[["Hits"]]
# Check if a and b are identical
identical(a,b)
Factors
# Load package and data
library(ISLR2)
data(Hitters)
# Determine the number of Leagues included in this variable
a <- Hitters$League
b <- levels(a)
length(b)
Tables
# Load package and data
library(ISLR2)
data(Hitters)
# Calculate the number of players per League using the table() function
table(Hitters$League)
Matrices 1
x <- matrix(c(7,8,5,3), 2, 2)
Matrices 2
x <- matrix(data= c(1,2,3,4), nrow =2, ncol = 2, byrow = TRUE)
Matrices 3
x <- matrix(data = c(1, 2, 3, 4), nrow = 2, ncol = 2)
, x.sqrt <- sqrt(x)
x.squared <- x^2
Indexing matrices 1
A <- matrix (1:16, 4, 4)
A1 <- A[1:3, 1:3]
A2 <- A[c(1,3),c(2,4)]
A3 <- A[2,4]
A4 <- A[,4]
A5 <- A[1,]+A[2,]
Indexing matrices 2
B <- matrix(1:8, 4, 2)
dim(B)
2.8.1-5. Vectoren
temp <- c(35,88,42,84,81,30)
city <- c("Beijing","Lagos","Paris","Rio de Janeiro","San Juan","Toronto")
named_temp <- temp
names(named_temp) <- city
temp_13 <- named_temp[1:3]
temp_ps <- named_temp[c("Paris","San Juan")]
2.8.6. Vector van getallen 12..73
12:73
Sequencing
x1 <- seq(5,10)
x2 <- seq(1,10, length = 4)
x3 <- seq(10,25,5)
2.8.7. Oneven getallen
seq(1,100,2)
Adding vectors and vector length
x <- c(1, 6, 2)
y <- c(1, 4, 3)
x.length <- length(x)
Google is your best friend:
max(1,20,15,13,17,18,12,5)
2.3.1 Som van integers 1,…,100
100*(100+1)/2
2.3.2 Som van integers 1,…,1000
1000*(1000+1)/2
2.3.3 Interpreteer code
2
Functions
x <- c(5,6,1,2)
Geneste functies
log10(sqrt(100))
Variables in a dataframe
2
Variables names
# Load package and data
library(ISLR2)
data(Hitters)
names(Hitters)
# Use the function names to extract the variable names
Examining variables
# Load package and data
library(ISLR2)
data(Hitters)
# Use the accessor to extract Hits and assign it to the object a
a <- Hitters$Hits
# Determine the class of a
class(a)
Multiple ways to access variables
, # Load package and data
library(ISLR2)
data(Hitters)
# Use the accessor to extract Hits and assign it to a
a <- Hitters$Hits
# Use the square brackets to extract Hits and assign it to b
b <- Hitters[["Hits"]]
# Check if a and b are identical
identical(a,b)
Factors
# Load package and data
library(ISLR2)
data(Hitters)
# Determine the number of Leagues included in this variable
a <- Hitters$League
b <- levels(a)
length(b)
Tables
# Load package and data
library(ISLR2)
data(Hitters)
# Calculate the number of players per League using the table() function
table(Hitters$League)
Matrices 1
x <- matrix(c(7,8,5,3), 2, 2)
Matrices 2
x <- matrix(data= c(1,2,3,4), nrow =2, ncol = 2, byrow = TRUE)
Matrices 3
x <- matrix(data = c(1, 2, 3, 4), nrow = 2, ncol = 2)
, x.sqrt <- sqrt(x)
x.squared <- x^2
Indexing matrices 1
A <- matrix (1:16, 4, 4)
A1 <- A[1:3, 1:3]
A2 <- A[c(1,3),c(2,4)]
A3 <- A[2,4]
A4 <- A[,4]
A5 <- A[1,]+A[2,]
Indexing matrices 2
B <- matrix(1:8, 4, 2)
dim(B)
2.8.1-5. Vectoren
temp <- c(35,88,42,84,81,30)
city <- c("Beijing","Lagos","Paris","Rio de Janeiro","San Juan","Toronto")
named_temp <- temp
names(named_temp) <- city
temp_13 <- named_temp[1:3]
temp_ps <- named_temp[c("Paris","San Juan")]
2.8.6. Vector van getallen 12..73
12:73
Sequencing
x1 <- seq(5,10)
x2 <- seq(1,10, length = 4)
x3 <- seq(10,25,5)
2.8.7. Oneven getallen
seq(1,100,2)
Adding vectors and vector length
x <- c(1, 6, 2)
y <- c(1, 4, 3)
x.length <- length(x)