Activity

Open up a new script, use all the proper headings and working directory settings, and do the following:

  1. Create a for loop that takes a vector of numbers vec <- c(1,2,3,4,5) and adds each number to the previous numbers to produce the output 1, 3, 6, 10, 15. This output may either be printed using print() or stored as a new vector.

  2. given the vector x <- c("Monday", "Sunday", "Wednesday", "Friday", "Thursday), use a ifelse() statement to categorize the days in x as either “weekend” or “weekday”.

  1. Turn each of the following code blocks into functions.Think about what each function does. Follow the specific guidelines in the lab manual to properly document, name, and assign arguments to the functions.

    function 1:

    function 2:

    x / sum(x, na.rm = TRUE)
    y / sum(y, na.rm = TRUE)
    z / sum(z, na.rm = TRUE)

    function 3:

    round(x / sum(x, na.rm = TRUE) * 100, 1)
    round(y / sum(y, na.rm = TRUE) * 100, 1)
    round(z / sum(z, na.rm = TRUE) * 100, 1)
  2. Create a function celsius_to_fahrenheit in R that takes a numeric or vector of temperatures in Celsius, and returns its equivalent in Fahrenheit. Follow the specific guidelines in the lab manual to properly document, name, and assign arguments to the functions (this is very important).

here is the mathematical formula for the conversion:

\[ \text{Fahrenheit} = \left( \frac{9}{5} \times \text{Celsius} \right) + 32 \]