# MAS115 Class 3 code/solutions
# Section 1 Loops etc
# The most basic loop
a <- 1:10
x <- 0
for(i in a) {
print(i)
x <- x + i
}
x
for(i in 1:10) {
print(i)
}
# If Else statements
x <- c(-3, 5, 12)
y <- rep(NA, length(x))
for(i in 1:length(x)) {
if(x[i] >= 0) {
y[i] <- x[i]
} else {
y[i] <- -x[i]
}
}
y
# Nested if ... else statements
x <- 6
if (x < 0) {
cat(x, "is less than zero \n")
} else if (x <= 4) { # Note it will only consider this if x < 0 is FALSE
cat(x, "is between 0 and 4 \n")
} else { # It will only consider this if both (x < 0) and (x <= 4) are FALSE
cat(x, "is bigger than 4 \n")
}
# Note it will only consider the statement for any block if all the previous statements are FALSE
# I.e. you only enter a single one of the blocks
# Conditions
x <- c(1,-2,3)
if(x < 0) print("Hi!")
x <- c(-1, 2, -3)
if(x < 0) print("Hi!")
# Booleans
?'&'
a <- c(TRUE, TRUE)
b <- c(FALSE, TRUE)
a&b
a|b
a&&b
a||b
(0>10) & ("a"/2)
(0>10) && ("a"/2)
# The break command
for(i in 1:10) {
print(i)
if(i == 7) break
}
i
# The next command (look carefully at the output)
for(i in 1:10) {
if(i == 7) next # Skip all lines below this and start next loop iteration
print(i)
}
# Section 2 Input
# try out
scan(n=1)
# compared with
scan(what=integer(),n=1)
# Try this!
x <- readline("Enter a word. ");print(nchar(x))
# Note that the plain text file can be created either inside or outside Rstudio.
grads <- read.table("Salary.dat", header=TRUE)
grads <- read.table("Salary.dat")
# Omitting "header=TRUE" shouldn't cause an error, but will misinterpret the file,
# by treating the header as part of the data.
#################### Section 3 Lab Tasks ################
## A for loop to print names of people
# Create vector with people's names
a <- c("Kate", "Bob", "Ioana")
# Use a for loop to add these names to the desired string to make a sentence
for(i in a) {
cat(i, "studies MAS115", "\n")
}
###############
## A for loop to count the sum of square numbers
###############
## Method 1 - writing your own function with loops
# Define the number of sum of squares you want to work out
n <- 10
# Create a vector in which to store the numbers
ssquares1 <- rep(NA,n)
# Initialise 1st entry to be 1^2
ssquares1[1] <- 1
# Now work out the others using a for loop
for(i in 2:n) {
ssquares1[i] <- ssquares1[i-1] + i^2 # add i^2 to previous value
}
ssquares1
########
## Method 2 - using the built in functions a bit
# Define the number of sum of squares you want to work out
n <- 10
# Create a vector in which to store the numbers
ssquares2 <- rep(NA,n)
for(i in 1:n) {
ssquares2[i] <- sum((1:i)^2)
}
ssquares2
#######################
# Calculating a function
#######################
# Create vector of x values
x <- seq(-1, 3, length = 1000)
# Create vector for f (initialised as NAs) which will fill up with values
f <- rep(NA, 1000)
# Use a for loop and nested if statements to split x into range and find f
for(i in 1:length(x)) {
if(x[i] < 0) {
f[i] <- 0
} else if(x[i] < 1) {
f[i] <- 3*x[i]
} else {
f[i] <- 5*x[i]^2 - 2
}
}
plot(x, f, type = "l")