Exercise 1
letters
## [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q"
## [18] "r" "s" "t" "u" "v" "w" "x" "y" "z"
s <- c(letters)
first.five <-s[1:5]
first.five
## [1] "a" "b" "c" "d" "e"
last.five <- s[(length(letters)-4):length (letters)]
last.five
## [1] "v" "w" "x" "y" "z"
Exercise 2
every.second <- s[seq(2,26,2)]
every.second
## [1] "b" "d" "f" "h" "j" "l" "n" "p" "r" "t" "v" "x" "z"
Exercise 3
s <- c(letters)
object.t <- c("t")
log.vec <- object.t ==s
log.vec
## [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [12] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
## [23] FALSE FALSE FALSE FALSE
which(log.vec)
## [1] 20
Exercise 4
vowels <- c("a", "e", "i", "o", "u")
log.vec2 <- s %in% vowels
log.vec2
## [1] TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE FALSE
## [12] FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
## [23] FALSE FALSE FALSE FALSE
which(log.vec2)
## [1] 1 5 9 15 21
Exercise 5
seeds <- c(6, 10, 243, 12, 43, 20, 34, 18, 24, 20)
pirates <- c("Emmanuel", "Alissa", "Lucia", "Marcel", "Florian", "Nadiia", "Yvonne", "Florina", "Xu", "Zoe")
over.thirty <- seeds > 30
over.thirty
## [1] FALSE FALSE TRUE FALSE TRUE FALSE TRUE FALSE FALSE FALSE
sum(over.thirty)
## [1] 3
mean(over.thirty)
## [1] 0.3
Exercise 6
less.fourty <- seeds < 40
sd(less.fourty)
## [1] 0.421637
Exercise 7
max.seeds <- max(seeds)
max.seeds
## [1] 243
log.most.seeds <- seeds >= max.seeds
log.most.seeds
## [1] FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
name.most <- which (log.most.seeds)
name.most
## [1] 3
pirates[name.most]
## [1] "Lucia"
Exercise 8
over.fifty <- seeds > 50
over.fifty
## [1] FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
pirate.number <- which(over.fifty)
pirates[pirate.number]
## [1] "Lucia"
Exercise 9
min.standard <- mean(seeds) - sd(seeds)*2
min.standard
## [1] -99.2736
max.standard <- mean(seeds) + sd(seeds)*2
max.standard
## [1] 185.2736
high <- seeds >max.standard
high
## [1] FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
low <- seeds < min.standard
low
## [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
is.outlier <- high | low
is.outlier
## [1] FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
There ist 1 outlier.
Exercise 10
no.outlier <- is.outlier == F
f <- which(no.outlier)
mean.no.outlier <- mean (seeds[f])
mean.no.outlier
## [1] 20.77778
median.no.outlier <- median (seeds[f])
median.no.outlier
## [1] 20
Exercise 11
katharina <- c("Breaking Bad", "Game of Thrones", "The Simpsons")
tani <- c("Orange is the New Black", "Game of Thrones", "House of Cards")
alexander <- c("The Walking Dead", "Game of Thrones", "True Detective")
sarah <- c("Mad Men", "My Little Pony", "House of Cards")
rebekka <- c("Game of Thrones", "Gotham", "Breaking Bad")
responses <- c(katharina, tani, alexander, sarah, rebekka)
responses
## [1] "Breaking Bad" "Game of Thrones"
## [3] "The Simpsons" "Orange is the New Black"
## [5] "Game of Thrones" "House of Cards"
## [7] "The Walking Dead" "Game of Thrones"
## [9] "True Detective" "Mad Men"
## [11] "My Little Pony" "House of Cards"
## [13] "Game of Thrones" "Gotham"
## [15] "Breaking Bad"
Exercise 12
favs <- unique(responses)
favs
## [1] "Breaking Bad" "Game of Thrones"
## [3] "The Simpsons" "Orange is the New Black"
## [5] "House of Cards" "The Walking Dead"
## [7] "True Detective" "Mad Men"
## [9] "My Little Pony" "Gotham"
Exercise 13
number.favs <- table(responses)
number.favs
## responses
## Breaking Bad Game of Thrones Gotham
## 2 4 1
## House of Cards Mad Men My Little Pony
## 2 1 1
## Orange is the New Black The Simpsons The Walking Dead
## 1 1 1
## True Detective
## 1
Exercise 14
show <- c("Orange is the New Black")
show
## [1] "Orange is the New Black"
show %in% responses
## [1] TRUE
sum(show == responses) > 0
## [1] TRUE
Exercise 15
house <- c("House of Cards")
sum(house == responses)
## [1] 2
l<- house == responses
l
## [1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
## [12] TRUE FALSE FALSE FALSE