Instructions

The goal of this assignment is to continue our exploration of vectors, get you started with working in R, and introduce you to some common operations. Note: you may need to search the internet to find answers for some of the questions.

You will be graded as follows:

  • Does your R Script run (some errors are acceptable in this assignment)?
  • Have you completed the assignment in its entirety?
  • Have you followed the instructions carefully?
  • Have you responded to the questions correctly?

Grading

  • (20 pts) Have you completed the assignment in its entirety?

  • (20 pts) Are your responses correct for the subset of randomly graded questions?

  • (10 pts) CODE DOCUMENTATION. Add comments to nearly every line explaining what the line of code is doing.


Feel free to use the below tabs to check out some common functions.

Tables

Arithmetic

Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
^ or ** Exponentiation
%% Modulus/remainder (5 %% 2 is 1)
%/% Integer division (5 %/% 2 is 2)

Logical

Operator Description
<, <= Less than (or equal to)
>, >= Greater than (or equal to)
== Equal to
!= NOT equal to
!x NOT x
x &#124; y x OR y
x & y x AND y

Summary

Function Description
max Maximum
min Minimum
range Range (min and max)
prod Product
sum Sum
any Any TRUE?
all All TRUE

Math functions

Function Description
abs Absolute value
sign Sign (positive or negative)
sqrt Square root
ceiling Round UP
floor Round DOWN
round Round
trunc Same as floor
cumsum Cumulative sum
cumprod Cumulative product
cummin Cumulative minimum
cummax Cumulative maximum
log Logarithm
exp Exponential/anti-log (e^?)
pi The constant, ‘PI’

Questions

Part 1

Q1

Part A (To be completed in Lab 1B)

This question was extracted from Lab 1B. Using the code you saved from the lab, proceed to part B.

Lab 1B asked the following:

Using the penguins data from the palmerpenguin package, create new vectors for the following variables: bill_length_mm, bill_depth_mm, flipper_length_mm, body_mass_g, island, sex, and species. For each vector, find the appropriate statistics for the appropriate variable type, i.e. sum, mean, and standard deviation for numeric vectors and frequencies for character variables.

Part B

  1. Create the following vectors:

    1. A vector containing the element-wise product of bill_length_mm and bill_depth_mm.
    2. A vector containing the element-wise addition of flipper_length_mm and body_mass_g.
    3. A vector containing the element-wise subtraction of bill_length_mm and body_mass_g.
    4. A vector containing the element-wise addition of bill_length_mm, bill_depth_mm, flipper_length_mm and body_mass_g.
  2. For the vectors found in question 1, find the following descriptive statistics: max, min, sum, mean, median, and standard deviation.

  3. Create a 2 by 2 table showing the cross-tabulation of island and species.

  4. Create a 2 by 2 table showing the table proportions of island and species.


Part 2

Q2

Please read the “Debugging techniques” of Hadley Wickham’s “Advanced R” (freely available online). Describe the 4 steps to debugging your code. Record your results as comments on your R Script.

Q3

Repair the code below to print out the R object my_vector. Record your results as comments on your R Script.

vec_1 <- c(4,4,)
## Error in c(4, 4, ): argument 3 is empty
vec_2 < c(5,6)
## Error in eval(expr, envir, enclos): object 'vec_2' not found
My_vector <- vec_1 * vec_2
## Error in eval(expr, envir, enclos): object 'vec_1' not found
print(my_vector)
## Error in print(my_vector): object 'my_vector' not found

Q4

Describe two methods to open the help documentation in R? Record your results as comments on your R Script.

Q5

Other than the library function, what are two other methods to call a function from an R Package?

Record your results as comments on your R Script.

Q6

Repair the following code to print out the value 10:

my_variable <- 10
my_varıable
## Error in eval(expr, envir, enclos): object 'my_varıable' not found

Q7

Describe what the functions sum and cumsum accomplishes:

x <- c(1:10)
sum(x)
cumsum(x)

Q8

Describe what the functions prod and cumprod accomplishes:

y <- 1:10
prod(y)
cumprod(y)

Q9

Describe what the %% operator accomplish.

Record your results as comments on your R Script.

z <- 11:15
z %% 2 

Q10

Describe what the == operator accomplish.

Record your results as comments on your R Script.

z <- 11:15
(z %% 2 ) == 0