Install R and RStudio

Work through these practice exercises

1.

    1. Define a variable named ans_1a_x and set its value to 10, a variable named ans_1a_y and set its value to 5, and a variable named ans_1a_z and set its value to the sum of ans_1a_x and ans_1a_y. The line of code that you write to define ans_1a_z must include the + operator.
    1. Define a variable named ans_1b_x and set its value to "10", a variable named ans_1b_y and set its value to "5", and a variable named ans_1b_z and set its value to the sum of the numeric values of ans_1b_x and ans_1b_y. The line of code that you write to define ans_1b_z must include the + operator and the as.numeric() function.
    1. Define a variable named ans_1c and set its value to the logical result indicating if ans_1a_x is equal to ans_1a_y. The line of code that you write to define ans_1c must include the == operator.

2.

    1. Create a vector named ans_2a that contains the elements 2, 3, 5, 7, and 11.
    1. Create a list named ans_2b that contains the elements 1, 2, "a", "b" and TRUE.
    1. Create a data.frame named ans_2c that contains v1, v2, and v3 as defined below as columns. Make sure the column names of the data.frame you create are equal to the variable names v1, v2, and v3.
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)

3.

    1. Write a line of code that returns the third element from the vector named tmp defined in the following code chunk and store the result in a variable named ans_3a.
tmp <- c(0.24, 0.015, 1.34, -1.00, -0.15)
    1. Write a line of code that returns the second element from the list named tmp defined in the following code chunk and store the result in a variable named ans_3b. Be sure that the data type of ans_3b is character.
tmp <- list(0.5, "W", FALSE)
    1. Write a line of code that returns the column named v2 from the data.frame named tmp defined in the following code chunk and store the result in a variable named ans_3c. Be sure that the line of code that you write includes the $ operator.
tmp <- data.frame(v1=c(1, 2, 3),
                  v2=c('A', 'B', 'C'),
                  v3=c(TRUE, TRUE, FALSE))

4.

    1. For the following code chunk, what is the data type of the second element of tmp? Store your answer in a variable named ans_4a by copying, pasting, and uncommenting one of the commented out lines of code below.
element_1 <- "A"
element_2 <- 2
element_3 <- FALSE
tmp <- list(element_1, element_2, element_3)

# ans_4a <- "character"
# ans_4a <- "numeric"
# ans_4a <- "logical"
    1. For the following code chunk, what is the data type of the second element of tmp? Store your answer in a variable named ans_4b by copying, pasting, and uncommenting one of the commented out lines of code below.
element_1 <- "A"
element_2 <- 2
element_3 <- FALSE
tmp <- c(element_1, element_2, element_3)

# ans_4b <- "character"
# ans_4b <- "numeric"
# ans_4b <- "logical"

5.

    1. Consider the following code chunk:
x <- 5
for(i in c(1, 3, 5)) {
  x <- i + 2
}
  • What is the value of x after the loop has finished executing?

  • What is the value of i after the loop has finished executing?

  • Modify the code chunk such that the final value of x is 10 and i is 20.

    1. Consider the following code chunk:
x <- 10
y <- c(1, 2, 3)
while(x > 5) {
  y <- c(x, y)
  x <- x - 1
}
  • How many elements does y contain after the loop has finished?

  • What is the value of the final element in y and can you figure this out without executing the code?

    1. Consider the following code chunk:
if(x > y) {
  z <- 10
} else {
  z <- 2
}
  • What is the value of z if x <- 10 and y <- -10

  • What is the value of z if x <- -5 and y <- 5

  • What is the value of z if x <- 0 and y <- 0

    1. Consider the following code chunk:
for(i in 1:10) {
  x <- i * 2
  if(x > 5) {
    break
  }
}
  • How many times will this loop run?

  • What is the value of i and x when the loop stops?

    1. consider the following code chunk:
if(x == y) {
  z <- 1
} else if(x > y) {
  z <- 2
} else if(x < y) {
  z <- 3
}
  • What is the value of z if x <- 10 and y <- -10
  • What is the value of z if x <- -5 and y <- 5
  • What is the value of z if x <- 0 and y <- 0

6.

    1. Consider the following code chunk:
f <- function(x) {
  y <- x^2
  return(y)
}
z <- f(3)
  • What are the values of x, y and z?

    1. Consider the following code chunk:
f <- function(x) {
  y <- x^2
  return(y)
}

g <- function(x) {
  y <- x - 2
  return(y)
}

z <- f(g(4))
  • What are the values of x, y and z?

    1. Consider the following code chunk:
f <- function(x, y, z) {
  res <- x + y - z
  return(res)
}
  • Is f(2, 1, -4) a valid way to call the function f?
  • Is f(y=2, 1, -4) a valid way to call the function f?
  • Is f(y=2, 1, -4) a wise way to call the function f?
  • What is the wisest way to call the function f?