rm(list=ls())
# 1
ans_1a_x <- 10
ans_1a_y <- 5
ans_1a_z <- ans_1a_x + ans_1a_y
ans_1b_x <- "10"
ans_1b_y <- "5"
ans_1b_z <- as.numeric(ans_1b_x) + as.numeric((ans_1b_y))
ans_1c <- ans_1a_x == ans_1a_y
# 2
ans_2a <- c(2, 3, 5, 7, 11)
ans_2b <- list(1, 2, "a", "b", TRUE)
v1 <- c('I', 'I', 'I', 'I', 'II', 'II', 'II', 'II')
v2 <- c('a', 'a', 'b', 'b', 'c', 'c', 'd', 'd')
v3 <- c(-1.6297880,
-1.0738506,
0.0299236,
-1.5435811,
-0.5133278,
-1.4716107,
-1.1986316,
-1.5548207)
ans_2c <- data.frame(v1, v2, v3)
# 3
tmp <- c(0.24, 0.015, 1.34, -1.00, -0.15)
ans_3a <- tmp[3]
tmp <- list(0.5, "W", FALSE)
ans_3b <- tmp[[2]]
tmp <- data.frame(v1=c(1, 2, 3),
v2=c('A', 'B', 'C'),
v3=c(TRUE, TRUE, FALSE))
ans_3c <- tmp$v2
# 4
ans_4a <- "numeric"
ans_4b <- "character"
# 5
# I'm only posting solutions here to the questions that you
# can't get the answer to by simply running the code and
# checking the results.
# 5a
# Modify the code chunk such that the final value of x is 10
# and i is 20
# x <- 5
# for(i in c(10, 20)) {
# x <- i / 2
# }
# 5b
# The final element in `y` is `y[length(y)]` which is 3. You
# know it's 3 straight away because the function `c` always
# combines `x` and `y` by putting `y` at the end.
#c, d, e
# These can all be checked by simply running the code and
# inspecting the results.
# 6a
# inside the function, `x` is 3, `y` is 9, and `z` is
# undefined.
#
# outside the function, `x` and `y` are not defined, and `z`
# is 9.
# bb
# outside the functions, `x` and `y` are undefined, and `z`
# is 4
#
# inside `g`, `x` is 4, and `y` is 2
# inside `f`, `x` is 2, and `y` is 4
# 6c
# They are all valid ways of calling the function, but only
# wise way of calling the function would be to name all
# arguments so that it's clear to you and anybody else
# reading your code what's what. E.g.,
# f(x=2, y=1, z=-4)