# R lab session 2
# Tasks 1.1.1
# 1. For example
A <- matrix(c(1,9,14,5,21,36),nrow=2,byrow=TRUE)
# 2.
A <- rbind(c(1,9,14),c(5,21,36))
# 3.
A[2,,drop=FALSE]
# 4. We could specify drop=TRUE, but no need
A[,3]
#5.
A[A>15] <- NA
# Not asked for, but they should understand how to break this into steps.
# On its own, what is given by A>15 ? A boolean matrix.
A <- rbind(c(1,9,14),c(5,21,36))
A>15
# Tasks 1.3.2
# 1.
PlantTrial <- data.frame(Species=c("Green","Green","Blue","Blue"),
Treatment=c("A","B","A","B"),
Yield=c(10.1,15.5,2.3,7.6),
Height=c(8.2,12.3,9.1,20.3))
# 2.
# a) Using ?mtcars, qsec is the "1/4 mile time".
# Online search tells us that this is the time in seconds taken
# to travel 1/4 mile from a standing start.
# b)
head(mtcars)
# or
mtcars["Datsun 710","qsec"]
# gives us an answer of 18.61.
# c) numerical, using
str(mtcars)
# d)
mtcars[mtcars$cyl==4,]
# e) There are many ways, but one is
mtcars$mpg[mtcars$am==0]
# to get the relevant numbers, and so
mean(mtcars$mpg[mtcars$am==0])
# to get their mean. Alternatively
mean(mtcars[mtcars$am==0,"mpg"])
# f)
mtcars["Ferrari Dino",]
# Tasks 1.4.1
# 1.
sysamp <- runif(20)
# 2.
mylist <- list(a=sysamp,b=PlantTrial)
# 3.
mylist[1]
# gives a list of one vector.
# Compare
mylist[[1]]
list(mylist[[1]])
# 4.
mylist[[2]]