# # Unit 5 - Vectors # # Practice II # # Solutions should be based only # on the material studied thus far. # (1) # Given a non-empty vector vec, # produce a vector of all indices # where strings appear. vec <- c("A", NA, "B", NA, NA, "C") indices <- c() for (ind in 1:length(vec)) { if (!is.na(vec[ind])) { indices <- c(indices, ind ) } } print(indices) print(class(NA)) print(class(vec[2])) vec1 <- c(3, NA, 4, NA) print(vec1) print(class(vec1[2])) # (2) # Given two non-empty numeric # vectors vec1 and vec2, # print, for each value in vec1, # the value and a vector including # all indices in vec2 in which # the value appears. vec1 <- c(3, 5, 4, 2) vec2 <- c(8, 3, 2, 7) # (3) # Given two equal-length # and parallel vectors ids and ages, # produce a vector of all ids of persons # whose age is under 40, and print # the new vector. ids <- c(1111, 2222, 3333, 4444) ages <- c(2 6, 40, 41, 81) myIDS <- c() for (ind in 1:length(ids)) { if (ages[ind] < 40) { myIDS <- c(myIDS, ids[ind]) } } print(myIDS) # (4) # Given a non-empty vector vec, # print TRUE if vec is sorted in # a non-descending order, # or FALSE otherwise. vec <- c(7, 8, 9, 10) found <- TRUE for (ind in 1:(length(vec)-1)) { if (vec[ind+1] < vec[ind]) { found <- FALSE break } } print(found) 1, 2, 3, 4, 5, 6 1, 1, 2, 3, 4, 5 6, 5, 4, 3, 2, 1 6, 6, 5, 4, 3, 2 # (5) # Given a sorted numeric vector vec, # and a given number, # insert the number into vec such that # it remains sorted. # 1 vec <- c(1, 2, 3, 4) num <- 5 for (ind in 1:(length(vec) - 1)) { if (num <= vec[1]) { newVec <- c(num, vec) break } else if (num >= vec[length(vec)]) { newVec <- c(vec, num) break } else if (num >= vec[ind] & num <= vec[ind+1]) { section1 <- vec[1:ind] section2 <- vec[(ind+1):length(vec)] newVec <- c(section1, num, section2) break } } print(newVec) # 2 vec <- c(1, 2, 3, 4) num <- 5 ind <- 1 newVec <- c() while (length(newVec) == 0 & ind <= length(vec)) { if (num <= vec[1]) { newVec <- c(num, vec) } else if (num >= vec[length(vec)]) { newVec <- c(vec, num) } else if (num >= vec[ind] & num <= vec[ind+1]) { section1 <- vec[1:ind] section2 <- vec[(ind+1):length(vec)] newVec <- c(section1, num, section2) } ind <- ind + 1 } print(newVec) 3 7 8 10 c(3 5 7 8 10) # (6) # Produce 10 random numbers # in range 1:10 and print the # most frequent number. freqs <- rep(0, times = 10) for (iteration in 1:10) { randNum <- sample(1:10, size = 1) print(randNum) freqs[randNum] <- freqs[randNum] + 1 } maxFreq <- -1 for (ind in 1:length(freqs)) { if (freqs[ind] > maxFreq) { maxFreq <- freqs[ind] maxNum <- ind } } print(maxNum) # (7) # Given two vectors vec1 and vec2, # write a program that checks # whether vec2 is a section of vec1. # If it is the program will print the index # where this section begins in vec2. # Otherwise the program will print NA. # You may assume that vec2 can not appear # more than once in vec1. vec1 <- c(8, 5, 2, 3, 1, 6) vec2 <- c(2, 3, 1) found <- TRUE if (length(vec1) < length(vec2)) { found <- FALSE } else { found <- FALSE for (ind in 1:(length(vec1) - (length(vec2 - 1)))) { section <- vec1[ind:(ind+2)] allEqual <- TRUE for (ind2 in 1:length(section)) { if (section[ind2] != vec2[ind2]) { allEqual <- FALSE } } if (allEqual == TRUE) { found <- TRUE } } } print(found) x <- c(9, 5, 2) y <- c(9, 5, 2) allEqual <- TRUE for (ind in 1:length(x)) { if (x[ind] != y[ind]) { allEqual <- FALSE } } print(allEqual)