Lab-9_ Iterations

.pdf

School

University of California, Berkeley *

*We aren’t endorsed by this school

Course

33B

Subject

Statistics

Date

Apr 3, 2024

Type

pdf

Pages

14

Uploaded by ProfProton22246

Report
Practice writing simple loops Get familiar with the syntax of a for loop Get familiar with the syntax of a while loop Get familiar with the syntax of a repeat loop Write your code and content in a qmd (quarto markdown) file. You can use the provided source qmd file (bCourses). Name this file as lab09-first-last.qmd , where first and last are your first and last names (e.g. lab09-gaston-sanchez.qmd ). Submit both your qmd and HTML files to the corresponding assignment submission in bCourses. Please note that submitting only one of the files will result in an automatic 10% deduction. Also, if you submit the incorrect files you will receive no credit. In this lab, we review various constructs and idioms in R to handle iterative computations: for() loop while() loop repeat loop apply() , lapply() , sapply() functions sweep() As we saw in lecture, R comes with three main types of loops: for , while , and repeat . Let’s consider a simple scenario in which, given the input vector x = c(2, 4, 6, 8) , we want to obtain an output vector y by adding 1 to each element in x . In other words, we basically want y = x + 1 . Stat 33B Gaston Sanchez Lab-9: Iterations AUTHOR Learning Objectives General Instructions Introduction 1 Recap: for , while , and repeat loops
As you know, we can use vectorized code y = x + 1 . However, for the sake of illustration, let’s ignore vectorization, and instead use loops. 1.1 for() loop recap # input object x = c ( 2 , 4 , 6 , 8 ) # initialize output object y = rep ( 0 , length (x)) # iterations for (i in 1 : length (x)) { y[i] = x[i] + 1 } 1.2 while() loop recap # input object x = c ( 2 , 4 , 6 , 8 ) # initialize output object y = rep ( 0 , length (x)) # initialize auxiliary iterator i = 1 # iterations while (i <= length (x)) { y[i] = x[i] + 1 i = i + 1 # increase iterator } 1.3 repeat loop recap # input object x = c ( 2 , 4 , 6 , 8 ) # initialize output object y = rep ( 0 , length (x)) # initialize auxiliary iterator i = 1 # iterations
Consider the following vector LTRS which contains various letters: Write a for() loop to count the number of letters in LTRS that are equal to "A" or "E" . How many "A" or "E" are in LTRS ? [1] 8 repeat { y[i] = x[i] + 1 i = i + 1 # increase iterator if (i > length (x)) { break } } 2 Counting letters LTRS <- c ( 'U' , 'E' , 'S' , 'Q' , 'A' , 'U' , 'P' , 'W' , 'C' , 'R' , 'Z' , 'V' , 'S' , 'Y' , 'Z' , 'T' , 'D' , 'E' , 'K' , 'D' , 'U' , 'V' , 'V' , 'W' , 'Y' , 'R' , 'B' , 'U' , 'D' , 'Q' , 'J' , 'J' , 'W' , 'W' , 'L' , 'O' , 'P' , 'V' , 'D' , 'B' , 'N' , 'D' , 'Y' , 'Z' , 'C' , 'G' , 'X' , 'I' , 'M' , 'X' , 'O' , 'G' , 'B' , 'E' , 'Q' , 'G' , 'U' , 'K' , 'J' , 'A' , 'L' , 'J' , 'X' , 'C' , 'H' , 'G' , 'K' , 'G' , 'T' , 'Y' , 'X' , 'M' , 'E' , 'D' , 'E' , 'T' , 'W' , 'X' , 'O' , 'Y' , 'D' , 'D' , 'F' , 'K' , 'G' , 'C' , 'I' , 'D' , 'V' , 'M' , 'D' , 'D' , 'P' , 'T' , 'Y' , 'A' , 'C' , 'D' , 'H' , 'Q' ) 2.1 Your Turn: for() loop # your code i = 0 for (x in LTRS) { if(x == "A" | x == "E" ) { i = i + 1 } } i 2.2 Your Turn: while() loop
Write a while() loop to count letter "D" in LTRS , until obtaining its 10th occurrence. How many iterations were necessary to get the 10th occurrence of "D" ? Hint : recall that break allows you to stop a loop from iterating. [1] 92 Repeat 2.2) but now using a repeat loop. Hint : recall that break allows you to stop a loop from iterating. In this part we want to show you some interesting and convenient functions in R for applying a function to the elements of various kinds of objects. # your code i = 1 c = 0 while (c < 10 ) { if (LTRS[i] == "D" ) { c = c + 1 } i = i + 1 } i 2.3 Your Turn: repeat loop # your code c <- 0 i <- 1 repeat { if (c == 10 || i > length (LTRS)) { break } if (LTRS[i] == "D" ) { c = c + 1 } i = i + 1 } 3 apply() and sweep() functions
apply() : apply a function to the elements of an array (e.g. a matrix ) lapply() : apply a function to the elements of a list sapply() : simplified apply a function to the elements of a list Consider the following matrix (based on data frame mtcars ) mpg disp hp Mazda RX4 21.0 160.0 110 Mazda RX4 Wag 21.0 160.0 110 Datsun 710 22.8 108.0 93 Hornet 4 Drive 21.4 258.0 110 Hornet Sportabout 18.7 360.0 175 Valiant 18.1 225.0 105 Duster 360 14.3 360.0 245 Merc 240D 24.4 146.7 62 Merc 230 22.8 140.8 95 Merc 280 19.2 167.6 123 A common statistical operation involves computing summary statistics (e.g. mean, median, min, max) of the variables in a table. You could use a for loop to calculate column-means: mpg disp hp 20.37 208.61 122.80 Instead of using a loop, you can also use apply() which allows you to apply a function to the columns, or the rows, or both cols-rows, of a matrix: mat is the first input 3.1 Example mat = as.matrix (mtcars[ 1 : 10 , c ( 'mpg' , 'disp' , 'hp' )]) mat # pre-allocate (i.e. initialize) vector of means col_means = c ( 0 , ncol (mat)) for (j in 1 : ncol (mat)) { col_means[j] = mean (mat[ ,j]) } names (col_means) = colnames (mat) col_means 3.2 apply() example
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help