Introduction

In this exercise, you will work on implementing various data structures in R.

Part A

  1. Create a vector using vec1 <- c(1L, 2L, 3L). What type of vector is this? Verify your answer using typeof(vec1).

  2. Run the following code: vec2 <- as.numeric(vec1). Compare vec1 and vec2 using typeof() and object.size(). What are the differences?

  3. Run the code: vec2 <- vec2 + c(1, 1.5, 1.99). Before checking the output, what is the result of this line of code?

  4. Coerce vec2 to integer-type. What happens to non-integers?

  5. Create another vector vec3 and assign the values "1L", "2L", "3L". What type of vector is this? Verify using typeof(). Coerce this to a numeric vector.

Part B

Open a new R script to write and save your code. You will need to re-use these results for Exercise 3!

  1. Create vectors using c() with the following attributes.

    1. A vector, name, with 10 names of your choosing. What type of vector is this?

    2. A vector, female, with TRUEs and FALSEs indicating the sex of the people in your name vector. What type of vector is this? What is another way we could represent this information?

    3. A vector, edu, indicating the highest degree of education completed for the people in your name vector. Use "HS" for high school, "BS" for a bachelor’s degree, "MS" for master’s, and "PhD" for doctorate.

    4. A vector, salary, indicating the salary in 1,000s of dollars for the people in your name vector.

  2. Run the following code to create a data.frame, data.

data <- data.frame(name = name,
                   female = female,
                   edu = edu,
                   salary = salary)