This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

Indexing and comparing vectors

Question 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"
letters [1:5]
## [1] "a" "b" "c" "d" "e"
a <- length(letters)
last.five <- letters[(a-4) :a]
last.five
## [1] "v" "w" "x" "y" "z"

Question 2

every.second <- letters[seq(from = 2, to = 26, by = 2)]
every.second
##  [1] "b" "d" "f" "h" "j" "l" "n" "p" "r" "t" "v" "x" "z"

Question 3

finding.t. <- letters == "t"
finding.t.
##  [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(finding.t.)
## [1] 20

Question 4

finding.vowels. <- c("a", "e", "i", "o", "u")
letters %in% finding.vowels.
##  [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 (letters %in% finding.vowels.)
## [1]  1  5  9 15 21

Question 5

seeds <- c(6, 10, 243, 12, 43, 20, 34, 18, 24, 20)
seeds
##  [1]   6  10 243  12  43  20  34  18  24  20
pirates <- c( "Emanuel", "Alissa", "Lucia", "Marcel", "Florian", "Nadiia", "Yvonne", "Florina", "Xu", "Zoe")

log.vec <- seeds > 30 # how many pirates bird ate over 30 seeds
log.vec
##  [1] FALSE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE
mean(c(seeds) > 30) # for percentage
## [1] 0.3

Question 6

log.vec <- seeds < 40
seeds [log.vec]
## [1]  6 10 12 20 34 18 24 20
sd(seeds [log.vec])
## [1] 8.815571

Question 7

max(seeds)
## [1] 243
pirates <- c( "Emanuel", "Alissa", "Lucia", "Marcel", "Florian", "Nadiia", "Yvonne", "Florina", "Xu", "Zoe")

max.seeds1 <- seeds == max(seeds)
max.seeds1
##  [1] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
which(max.seeds1)
## [1] 3
pirates [which(max.seeds1)]
## [1] "Lucia"
min.seeds1 <- seeds == min(seeds)
min.seeds1
##  [1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
which(min.seeds1)
## [1] 1
pirates [which(min.seeds1)]
## [1] "Emanuel"

Question 8

log.more <- seeds > 50
pirates [which(log.more)]
## [1] "Lucia"

Question 9

sd(seeds)
## [1] 71.1368
mean(seeds)
## [1] 43
outlier <- seeds < mean(seeds) - 2*sd(seeds) | (seeds > mean (seeds) + 2*sd(seeds))
outlier
##  [1] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
is.outlier <- which (outlier)
is.outlier
## [1] 3

Question 10

mean(seeds[-is.outlier])
## [1] 20.77778
median(seeds[-is.outlier])
## [1] 20

Question 11

Question 11

Question 12

Question 13

Question 14

Question 15

Question 16

Question 17 `