# # Unit 7 - Vectorization # # Practice # gender <- c("M", "F", "F", "F", "M") educ <- c(12, 12, 15, 10, 13) salary <- c(11912.44, 10753.82, 5113.94, 8883.05, 6532.11) # (1) # How many female employees are there? gender <- c("M", "F", "F", "F", "M") print(length(gender[gender == "F"])) # (2) # What is the average years # of education of males? gender <- c("M", "F", "F", "F", "M") educ <- c(12, 12, 15, 10, 13) maleEduc <- educ[gender == "M"] sumEduc <- 0 for (years in maleEduc) { sumEduc <- sumEduc + years } print(sumEduc / length(maleEduc)) # (3) # Increase all salaries by 2000, # then find how many salaries are larger than 10000. salary <- c(11912.44, 10753.82, 5113.94, 8883.05, 6532.11) bonusSalary <- salary + 2000 print(length(bonusSalary[bonusSalary > 10000])) # (4) # Produce a new vector, bonus: for each employee # it includes 1000 * number of education years. # Then replace with the calculated bonuses # each salary smaller than the bonus. educ <- c(12, 12, 15, 10, 13) salary <- c(15912.44, 10753.82, 5113.94, 8883.05, 6532.11) bonusSal <- 1000 * educ for (ind in 1:length(salary)) { if (salary[ind] < bonusSal[ind]) { salary[ind] <- bonusSal[ind] } } print(salary) # (5) # Produce a new vector: salCat # It includes: "A" - for each salary < 6000, # "B" - for each 6000 <= salary < 10000 # "C" - for each salary >= 10000 # Next produce a vector which includes the # number of salaries for each category "A", "B" and "C", # in this order. salary <- c(11912.44, 10753.82, 5113.94, 8883.05, 6532.11) #salCat <- salary salCat <- character(length(salary)) salCat[salary < 6000] <- "A" salCat[salary >= 6000 & salary < 10000] <- "B" salCat[salary > 10000] <- "C" freqs <- c() for (cat in sort(unique(salCat))) { freqs <- c(freqs, length(salCat[salCat == cat])) } print(freqs) print(salCat) # (6) # Produce a new vector: salCat # It includes: "A" - for each salary < 6000, # "B" - for reach salary >= 6000 # Then find whether among those employees whose # salaries are of category "B" there are # both men and women. gender <- c("F", "F", "F", "F", "M") salary <- c(11912.44, 10753.82, 5113.94, 8883.05, 1532.11) salCat <- ifelse(salary < 6000, "A", "B") if (length(unique(gender[salCat == "B"])) == 2) { print("yes") } else { print("no") } # (7) Write the function Q9. # (a) # Add 2000 to all salaries of men. gender <- c("M", "F", "F", "F", "M") salary <- c(11912.44, 10753.82, 5113.94, 8883.05, 6532.11) salary[gender == "M"] <- salary[gender == "M"] + 2000 # (b) # The function has four parameters: # - vec1 - a non-empty numeric vector # - vec2 - a non-empty nominal vector # whose length is equal to vec1's # length and which represents categories # of the values in vec1 # - cat - a label in vec2 # - num - a numerical value # The function adds num to all vec1's values # if they are of category cat in vec2. # Next Use the function Q9 to increase the salaries # of all men by 2000. Q7 <- function(vec1, vec2, cat, num) { vec1[vec2 == cat] <- vec1[vec2 == cat] + num return(vec1) } gender <- c("M", "F", "F", "F", "M") salary <- c(11912.44, 10753.82, 5113.94, 8883.05, 6532.11) educ <- c(12, 12, 15, 10, 13) print(Q7(salary, gender, "M", -1000))