# # Unit 7 - Vectorization # # Filtering vectors # # Problem # Find all names of students # whose grade is under 60. names <- c("Yasmin", "Bella", "Adam", "Ruth") grades <- c(52, 93, 82, 41) myNames <- c() for (ind in 1:length(grades)) { if (grades[ind] < 60) { myNames <- c(myNames, names[ind]) } } print(myNames) # Here to R's vectorization provides # a more efficient code. # Note the new context of using # the [ ] operator names <- c("Yasmin", "Bella", "Adam", "Ruth") grades <- c(52, 93, 82, 41) under60 <- names[grades < 60] print(under60) # A deeper look on the above code. # We know what this does. names <- c("Yasmin", "Bella", "Adam", "Ruth") result <- names[c(1, 4)] print(result) # Now look at these. names <- c( "Yasmin", "Bella", "Adam", "Ruth") result <- names[c(FALSE, TRUE, TRUE, FALSE)] print(result) names <- c("Yasmin", "Bella", "Adam", "Ruth") result <- names[grades < 60] print(result) # Now the condition we use within # the brackets, produces a boolean # vector. grades <- c(52, 93, 82, 41) vec <- grades < 60 print(vec) # So actually what we are doing here: names <- c("Yasmin", "Bella", "Adam", "Ruth") grades <- c(52, 93, 82, 41) under60 <- names[grades < 60] # is this names <- c("Yasmin", "Bella", "Adam", "Ruth") under60 <- names[c(TRUE, FALSE, FALSE, TRUE)] print(under60) # Problem: # Find all names of students whose # grade is above 50 and under 90. names <- c("Yasmin", "Bella", "Adam", "Ruth") grades <- c(52, 93, 82, 41) myNames <- names[grades > 50 & grades < 90] print(myNames) vec1 <- grades > 50 print(vec1) vec2 <- grades < 90 print(vec2) print(vec1 & vec2) print(vec1 | vec2) # Problem: # Find all names of students whose # grade is NOT (above 50 and under 90). names <- c("Yasmin", "Bella", "Adam", "Ruth") grades <- c(52, 93, 82, 41) myNames <- names[!(grades > 50 & grades < 90)] print(myNames) # Now look at this code: # What happens if the vectorization # is carried out on vectors of different # lengths? names <- c("Yasmin", "Bella", "Adam", "Ruth") grades <- c(52, 93, 82) under60 <- names[grades < 60] print(under60) # Problem: # Find all names of students who received # a grade in the following group of grades. names <- c("Yasmin", "Bella", "Adam", "Ruth") grades <- c(52, 93, 82, 58) gradeGroup <- c(52, 82, 93) myNames <- names[grades %in% gradeGroup] print(grades %in% gradeGroup) # Finally: replacement # Problem: # Set as 100 the grades of all students # in third year. year <- c(1, 2, 3, 3) grades <- c(52, 93, 82, 58) grades[year == 3] <- 100 print(grades) year <- c(1, 2, 3, 3) grades <- c(52, 93, 82, 58) grades[year == 3] <- c(100, 100) year <- c(1, 2, 3, 3) grades <- c(52, 93, 82, 58) grades[year == 3] <- c(100, 100) # Increase by 5 the grades of all students # in third year. year <- c(1, 2, 3, 3) grades <- c(52, 93, 82, 58) grades[year == 3] <- grades[year == 3] + 5 # Last problem: # Replace with 100 each grade > 90, # and with 0 each grade <= 90 # 1 grades <- c(52, 93, 82, 58) grades[grades > 90] <- 100 grades[grades <= 90] <- 0 print(grades) # 2 grades <- c(52, 93, 82, 58) grades <- ifelse(grades > 90, 100, 0) print(grades) print(ifelse(grades > 90, 100, 0) )