# Michael Alfaro EEB 201 Exercise 1
# Joanna Wu
# Due 10/4/21
# 1. Print 9 dots using a for loop
for(i in 1:9) {cat('•\n')}
## •
## •
## •
## •
## •
## •
## •
## •
## •
# 2. Modify your for loop so that it prints 10 asterisks,
# with each asterisk separated by exactly one ampersand sign (&),
# with no spaces or new line characters.
for(i in 1:10) {cat('*&\n')}
## *&
## *&
## *&
## *&
## *&
## *&
## *&
## *&
## *&
## *&
# 3. by hand, figure out the initial values of these variables and values at
# the start and end of each iteration of the loop
dogs <- 10;
for (i in 1:5){
dogs <- dogs + 1; }
# initial value = 10 because that's the the value you start with
# value after iteration:
# 1: 11
# 2: 12
# 3: 13
# 4: 14
# value at the end = 15 (5 was added sequentially)
###
meatloaf <- 0; for (i in 5:9){
meatloaf <- meatloaf - i + 1;
cat(meatloaf) }
## -4-9-15-22-30
# initial value = 0
# value when i =
# 5: -4 (0-5+1 = -4)
# 6: -4-6+1 = -9
# 7: -9-7+1 = -15
# 8: -15-8+1 = -22
# 9: -22-9+1 = -30 (ending value)
###
bubbles <- 12
for (i in -1:-4){
bubbles <- i
cat(bubbles) # check
}
## -1-2-3-4
# initial value = 12 to start though it's never used
# first value = -1
# second value = -2
# third value = -3
# ending value = -4 as the last value in the vector
# 4. modify this code so that it will print out a message during presidential
# as well as congressional election years
years <- c( 2015, 2016, 2018, 2020, 2021)
for(ii in 1:length(years)){
if(years[ii] %% 2 == 0){ # if years are divisible by 2
cat(years[ii], 'Hooray, congressional elections!', sep = '\t', fill = T)
}
if(years[ii] %% 4 == 0){ # if years are divisible by 4
cat(years[ii], 'Presidential elections are kind of a big deal!', sep = '\t', fill = T)
}
}
## 2016 Hooray, congressional elections!
## 2016 Presidential elections are kind of a big deal!
## 2018 Hooray, congressional elections!
## 2020 Hooray, congressional elections!
## 2020 Presidential elections are kind of a big deal!
# 5. More fun with loops. Here are the bank accounts from seven randomly
# selected UCLA grad students
bankAccounts <- c(10, 9.2, 5.6, 3.7, 8.8, 0.5);
# Now look at the error message the following lines of code produce.
# Can you think of a way to modify this
# interestRate <- 0.0125;
# for (i in 1:length(bankAccounts)) {
# compounded[i] <- interestRate*bankAccounts[i] + bankAccounts[i]; }
# HINT: variables must be initialized before you can perform operations on them
# HINT 2: look at the rep() function and see if you can use that to initialize a
# initialize a vector called compounded
compounded <- rep(NA, 6)
# run the loop
for (i in 1:length(bankAccounts)) {
compounded[i] <- interestRate*bankAccounts[i] + bankAccounts[i]; }
compounded # now returns the bank accounts with interest rates compounded
## [1] 10.52500 9.68300 5.89400 3.89425 9.26200 0.52625
# 6. Go back to the compounded interest example. Suppose we now want to compound
# the interest annually, but across a period of 5 years. The for loop we
# discussed earlier only compounds for a single year. Try this:
bankAccounts <- c(10, 9.2, 5.6); #define bank accounts here
interestRate <- 0.0525;
house <- c(4.8, 3.8, 5.7); #deduct
food<- c(3.5, 4.3, 5.0); #deduct
fun <- c(7.8, 2.1, 10.5); #deduct #and incomes (through TAships) of
income <- c(21, 21, 21); #add this
# write loop
for (j in 1:5) { # years 1-5
for(i in 1:length(bankAccounts)){
#step 1 modify bankAccounts so that amounts reflect income and expenses
bankAccounts[i] <- bankAccounts[i] - house[i] - food[i] - fun[i] + income[i]
#step 2 get calculate interest and add to accounts from step 1
#you can actually use the line you have already written if you
#modify amounts in bankAccounts directly in step 1
bankAccounts[i] <- interestRate*bankAccounts[i] + bankAccounts[i]
}
}
bankAccounts
## [1] 41.55520 75.00653 6.06370
# 7) Three students have estimated annual expenditures for food, housing,
# and fun of: (in thousands of dollars) Modify the 5-year interest-compounding
# code from #5 and #6 so that it runs from 2020-2025 and so that in odd numbered
# years students 1 and 3 get trust fund disbursements of $5000.
# (hint the modulus function %% will be helpful )
# define variables
house <- c(4.8, 3.8, 5.7)
food<- c(3.5, 4.3, 5.0)
fun <- c(7.8, 2.1, 10.5)
#and incomes (through TAships) of
income <- c(21, 21, 21)
# reset values of bank accounts
bankAccounts <- c(10, 9.2, 5.6); #define bank accounts here
# add disbursements for students 1 and 3
funds <- c(5, 0, 5)
# loop through all 5 years
for(j in 2020:2025){
for(i in 1:length(bankAccounts)){
if(j %% 2 == 1){
# add $5000 in odd years for students 1 and 3 (funds[i])
bankAccounts[i] <- bankAccounts[i] + funds[i]}
bankAccounts[i] <- bankAccounts[i] - house[i] - food[i] - fun[i] + income[i]
bankAccounts[i] <- interestRate*bankAccounts[i] + bankAccounts[i]
}
}
bankAccounts
## [1] 66.44391 90.31137 23.72135
# 8) use a while loop to sum all numbers from 1:17. You will need to use a
# counter variable (like index seen in class).
# while value <= 17 or < 18
ii <- 1
sum <- 0
while(ii < 18){
sum <- ii + sum # variable to contain the sum
ii <- ii + 1 # counter variable
}
cat(sum)
## 153
# Exercise 9) write a function takes a number, and prints ‘small’ if number less
# than or equal to -1; ‘medium’ if between -1 and + 1’big’ if greater than or equal to + 1
fun.num <- function(nn){
if(nn <= -1) {cat('small')}
if(nn >=-1 & nn <=1) {cat('medium')}
if(nn >1){cat('big')}
}
fun.num(-4)
## small
fun.num(0)
## medium
fun.num(1)
## medium
# knit into rmd file
# library(knitr)
# spin('~/Documents/EEB Orientation/alfaro_joannawu_exercise1.R')