Problem 11. From PSET 1
We didn’t get to this problem but go ahead and try it this time! Write an R program to convert temperatures to and from celsius, fahrenheit. You may need to look back to Lesson 3 to revisit if statements.
temp = c('32C')
degree = as.integer(substr(temp, 1, 2))
i_convention = substr(temp, 3, 3)
if (i_convention == "C") {
result = as.integer(round((9 * degree) / 5 + 32))
o_convention = "Fahrenheit" }
if (i_convention == "F") {
result = as.integer(round((degree - 32) * 5 / 9))
o_convention = "Celsius" } else {
print("Input proper convention.")}
## [1] "Input proper convention."
paste("The temperature in", o_convention, "is", result, "degrees.")
## [1] "The temperature in Fahrenheit is 90 degrees."
From lesson 5, we learned that while loops take this form:
while (boolean-expression) { block of instructions }
Problem 1
When given variables K = 5 and I = -2, write a while loop that prints the sum of I and K after repeatedly adding 2 to I and subtracting 1 from K until I is greater than K.
K <- 5
I <- -2
while (I <= K) {
print("I")
print(I)
print("K")
print(K)
I <- I + 2
K <- K - 1
print(I + K)
}
## [1] "I"
## [1] -2
## [1] "K"
## [1] 5
## [1] 4
## [1] "I"
## [1] 0
## [1] "K"
## [1] 4
## [1] 5
## [1] "I"
## [1] 2
## [1] "K"
## [1] 3
## [1] 6
Problem 2
When given the variable int = 4L, write a while loop that subtracts 1 from int and then calculates its cubed value until int becomes negative. Afterwards, plot the cubed values.
int_cubed <- c()
int = 4L
while (int >= 0) {
int <- int - 1
int_cubed <- c(int_cubed, int**3)
}
plot(int_cubed)
Problem 3
Print the following pattern using a while loop:
# *
# **
# ***
# ****
You will need to use the rep() function and the cat() function to be able to correctly print out this pattern.
i <- 1
while (i <= 4) {
ast <- rep("*", i)
i <- i + 1
cat(ast)
cat("\n")
}
## *
## * *
## * * *
## * * * *
Problem 4
Print the multiplication table of 50 (up until it’s 10th multiple of 50) using a while loop.
i = 1
while (i <= 10) {
print(50*i)
i = i + 1
}
## [1] 50
## [1] 100
## [1] 150
## [1] 200
## [1] 250
## [1] 300
## [1] 350
## [1] 400
## [1] 450
## [1] 500
Problem 5
Print a vector with the factors of 5 (till 50) using a while loop. Print a vector that prints these factors backwards.
temp <- 50
stored_multiples <- list()
while(temp >= 0) {
stored_multiples <- append(stored_multiples, temp)
temp <- temp -5
}
for (i in stored_multiples) {
cat(i, " ")
}
## 50 45 40 35 30 25 20 15 10 5 0
Print a vector that prints these values:
[1] 50 25 10 5
i <- 0
factors <- c()
while (i <= 10) {
i = i + 1
factor <- 50/i
if (50 %% i == 0) {
factors <- c(factors, factor)
}
}
print(factors)
## [1] 50 25 10 5
From lesson 5, we learned that repeat loops take a slightly different form than while loops. The boolean expression is at the bottom instead of the beginning of the loop:
repeat { block of instructions (boolean-expression) { break } }
Let’s try some repeat loop problems
Problem 6
Using a repeat loop, print out the following:
How many licks does it take to get to the center of a Tootsie Pop? 1 2 CRUNCH. The world may never know.
print('How many licks does it take to get to the center of a Tootsie Pop?')
## [1] "How many licks does it take to get to the center of a Tootsie Pop?"
i <- 1
repeat{
print(i)
i <- i + 1
if (i == 3) {
print('CRUNCH. The world may never know.')
break
}
}
## [1] 1
## [1] 2
## [1] "CRUNCH. The world may never know."
Problem 7
Repeat problem 3 but using a repeat loop. Problem 3 said, print this following pattern:
# *
# **
# ***
# ****
i <- 1
repeat {
ast <- rep("*", i)
i <- i + 1
cat(ast)
cat("\n")
if (i == 5) {
break
}
}
## *
## * *
## * * *
## * * * *
Problem 8
Starting at 1, print every odd number from 1-19.
i <- 1
repeat {
i <- i + 2
print(i)
if (i == 19) {
break
}
}
## [1] 3
## [1] 5
## [1] 7
## [1] 9
## [1] 11
## [1] 13
## [1] 15
## [1] 17
## [1] 19
Problem 9
Using a repeat loop, count how many days have past since the new year. Read up on the library lubridate and the function as_date to understand what is happening. Here is one source but check out others as well: (https://www.rdocumentation.org/packages/lubridate/versions/1.7.4/topics/as_date)
library(lubridate)
##
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
##
## date
date <- as_date("2020-01-01")
days_since_newyear <- 0
repeat {
days_since_newyear <- days_since_newyear + 1
date <- date + 1
if (date == today())
break
}
days_since_newyear
## [1] 98
library(lubridate)
date <- as_date("2020-01-01") #no longer just character
today <- today()
days_between <- 0
repeat {
# line below just increments the days by 1
date <- date %m+% days(1)
days_between = days_between + 1
if (date == today) {
break
}
}
print(days_between)
## [1] 98