#R code for MAS115 Sem 2, Lab Class 1
# 1.3 Commenting
x <- 1:10
x <- x + 1 # Add 1 to every element in x
# Find out the variance of a vector
EmpVar <- function(x)
{
n <- length(x) # Find the length of x
(sum(x^2) - n * mean(x)^2) / n # The `sample' variance formula ends with n-1 instead
}
# You can now try it out
z <- rnorm(1000, mean = 0 , sd = 2)
# What do you think the answer to this should be?
EmpVar(z)
# should give a variance of somewhere around 4
### 2.1 Creating simple objects
1/3
x <- 1/3
x
(x <- 1/3)
# 2.1.1 Variable modes
## A vector of double precision numbers
A <- c(1.2,2,3.4) # The c() command simply tacks the numbers together
## To create a vector of integers you need to force it
B <- as.integer(c(1,2,3))
## A vector of characters (needs quotes around each value)
C1 <- c("Tim", "Jane", "Kate") # R recognises that the quotes imply characters
C2 <- as.character( c("Tim", "Jane", "Kate")) # Or again you can force it
## A vector of logicals (again recognises these automatically)
D <- c(TRUE, FALSE, TRUE)
## A vector of factors (created in two possible ways - you'll see more on this later)
E1 <- factor(c("Year1", "Year2", "Year3"))
E2 <- as.factor(c("Year1", "Year2", "Year3"))
## Solutions to 2.1.2 tasks
#1.
Score <- 10.4
#2.
isMALE <- TRUE
#3.
Name <- "Tim"
### 2.2 Vectors
x <- c(0, 1.2, 8/5) # c is short for concatenate = chain together
y <- 1:10
t1 <- x > 2
t2 <- y == 4
## Solutions to 2.2.1 tasks
#1.
is.vector(Score)
#2.
set.seed(13)
height <- sample(150:160, size = 10, replace = TRUE)
height
#3.
Pest <- rep(c("A", "B"), c(3,7))
#4.
## Using the paste command, which basically adds together character vectors.
# For example try looking at the output of
paste("Axis", c("x", "y", "z"))
# You should see that this gives you a character vector of length 3,
# where Axis has had either x, y or z added to it.
# Note the recycling of the vector of length 1 i.e. "Axis"
# to fit the length of c("x", "y", "z").
# Hence to get the output asked for Flower you can use
Flower <- paste("Helianthus", rep(c("debilis", "annuus"), c(8,2)))
Flower
#5.
# Comparisons - just use a standard operator e.g. <, >, == and take
# advantage of the fact that R can vectorise things for you.
isTall <- height >= 155
isTall
## Manipulating Vectors
a <- 1:3
b <- 6:8
c <- 1:2
# Same length operation
a + b # gives 7 9 11
a * b
# Recycling
a + c # gives 2 4 4 because 1+1=2, 2+2=4 and 3+c[1]=4
a * c
a + 1
# Extracting elements of vectors
d <- 1:10
d[2]
d[-2] # note this removes the 2nd element
d[1:9]
# Replacing elements
d[1] <- 10
d
d[2:5] <- c(10,12,13,14)
d
## Solutions to 2.2.2 tasks
#1.
# Why is the command a+1 an example of recycling?
# Answer: because a is of length 3, and the value 1 is of length 1
# Hence R cycles through 1 three times.
#2.
height[isTall] # produces a vector of just the numbers >= 155
#3.
# Create a vector Aheight to contain just those values in vector 'height'
# that have corresponding 'Flower' value "Helianthus annuus".
(Aheight <- height[Flower == "Helianthus annuus"])
#4.
#a) First 100 even numbers in order
(Even=seq(from=2,to=200, by=2))
#b) Create subvector EvNoFirst which has removed 1st element of Even
(EvNoFirst <- Even[-1])
#c) Create subvector EvNoLast which has removed last element of Even
(EvNoLast <- Even[-100])
#d) Calculate the difference EvNoFirst - EvNoLast
EvNoFirst - EvNoLast
# all 2's