library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.2
## ✔ ggplot2 4.0.0 ✔ tibble 3.3.0
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.1.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# a)
heights = c(145.5, 189.7, 183, 135.3, 175, 178, 162, 102, 168.8, 195.2)
# Assign letters A to J to these heights
# First, print the vector "LETTERS" to be the same lenght as the vector "heights"
height_names <- c(LETTERS[1:10])
print(height_names) # Double check
## [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
# Secondly, assign the LETTERS to the vector "heights"
names(heights) <- c(height_names)
print(heights)
## A B C D E F G H I J
## 145.5 189.7 183.0 135.3 175.0 178.0 162.0 102.0 168.8 195.2
# A B C D E F G H I J
# 145.5 189.7 183.0 135.3 175.0 178.0 162.0 102.0 168.8 195.2
# b)
heights_eval = c(145.5, 189.7, 183, 135.3, 175, 178, 162, 102, 168.8, 195.2)
# Write down our bounds
small <- 150
tall <- 185
# Apply the bounds to "heights_eval"
is_short <- (heights_eval<small)
is_tall <- (heights_eval>tall)
is_pass <- (small<=heights_eval) & (heights_eval<=tall)
# Make an empty character vector the same length as "heights_eval" &
# rename the boolean variables into the new vector
height_level <- character(length(heights_eval))
height_level[is_short] <- "short"
height_level[is_pass] <- "pass"
height_level[is_tall] <- "tall"
# Assign the new vector to "heights_eval"
heights_eval <- height_level
heights_eval
## [1] "short" "tall" "pass" "short" "pass" "pass" "pass" "short" "pass"
## [10] "tall"
# [1] "short" "tall" "pass" "short" "pass" "pass" "pass" "short" "pass" "tall"
# c)
height_names[is_pass]
## [1] "C" "E" "F" "G" "I"
# [1] "C" "E" "F" "G" "I"
# d)
# Create a numeric vector of length "heights"
Entrance_Fee <- numeric(length(heights))
# Assign the fee ammount depending on height
Entrance_Fee[is_short] <- 3
Entrance_Fee[is_pass] <- 4
Entrance_Fee[is_tall] <- 5
# Calculate the sum
sum(Entrance_Fee)
## [1] 39
# [1] 39