### ==================== lab 1: basic R ====================
###
### This is your first lab. Write the code that performs
### the following tasks underneath each comment.
###
### Ensure that your code produces output as appropriate
### If just asked to assign a value to a variable, you do not
### need to show output. But when computing a value, you should
### show the result.
###
### The lab is not graded, so no problems if you cannot do everything.
### The most important tasks are:
### a) get some code to work
### b) get the report compiled
### c) understand what/how to submit.
### See 2.3.4 "compiling report" in the course book
### https://faculty.washington.edu/otoomet/info201-book/r-intro.html#r-basic-report
###
### Good luck!
## What is your name? Assign this to variable 'name'
name <- "Ott" # this does not produce output,
# it is not really needed here.
## What is your favorite place you'd like to be? Assign it to
## a suitable named variable
"A warm and sunny place" # wrong: not assigned to a variable
dreamPlace <- "A warm and sunny place" # this is assignment
## Combine your name you assigned above, and the place you want to be,
## into a string like
## "Hi, I am <name>, and I'd like to be in <place>"
paste("Hi, I am", name, "and I'd like to be in", dreamPlace)
# this combines, and produces output
# you'll see it when you compile the report
### 1. ---------- compute ----------
##
## Convert degrees F to degrees C
## The formula is c = (f - 32)/1.8
f <- 43
c = (f - 32)/1.8
## What is the temperature today? Assign it to a variable 'fahrenheit'
#43F
## Compute temperature in C and assign it to variable 'centigrade'
centigrade <- c
## show the output
centigrade
## [1] 6.111111
## Create a variable `puppies` equal to the number of puppies you'd like to
## have
puppies <- 10
## Create a variable `price`, which is how expensive you think a puppy is
price <- 18
## Create a variable `total` that has the total cost of all of your
## puppies
total <- price * puppies
## How many puppies you can afford for $1K?
## Compute this in R, not just assign!
## Hint: use integer division %/%
puppiesfor1k <- 1000 %/% price
puppiesfor1k
## [1] 55
### 2. ---------- text ----------
###
## Create a variable `hometown` that stores the city in which you were born
hometown <- "Olympia"
## Assign your name to the variable `name`
name <- "Kady"
## Load `stringr` library. You may have to install it if you
## haven't done it yet.
## Use function `str_c` from stringr package to combine your name and
## your hometown into a greeting message. It should looks like
## "My name is Ott and I am from Tartu".
cat("My name is", name, "and I am from", hometown)
## My name is Kady and I am from Olympia
## 3. ----- Logical variables
## Create a boolean variable `too.expensive`, set to true if the cost
## of all puppies you want is greater than $1,000
too.expensive <- total > 1000
## Now let's do weather forecast:
## assign TRUE or FALSE to a variable named 'I_saw_raindrops'
I_saw_raindrops <- TRUE
## assign TRUE or FALSE to a variable named 'my_shoes_are_wet'
my_shoes_are_wet <- FALSE
## assign TRUE or FALSE to a variable named 'my_professor_is_soaked'
my_professor_is_soaked <- FALSE
## compute chance of rain in Seattle today: add all these three variables and
## divide by 3. This is your predicted rain probability.
## Show the result with an explanatory message!
##
## Reminder: the result should be between 0 and 1!
weatherForecast <- (I_saw_raindrops + my_shoes_are_wet + my_professor_is_soaked) / 3
## more complex logical expressions
## Is it a sad day? The day is sad if
## puppy's price is more than $100, and the probability of rain is > 0.5.
## Use the variables you computed above, and
## a logical expression, containing logical "and" to compute it.
puppyPrice <- price > 100
sadDay <- puppyPrice && weatherForecast
sadDay
## [1] FALSE
## given your height in cm, convert it into feet, in
## You may want to use integer division %/% and modulo %% operators
## For instance 178cm should give 5'10"
## Ensure you print it in a meaningful way!
heightInCm <- 160.5
feet <- heightInCm / 30.48
cat("Your height in cm converted to feet is", feet)
## Your height in cm converted to feet is 5.265748
## Finally, compile your work as html report and submit on canvas!
## You can compile it from the menu: File > Compile Report...
## It is not graded, but this is how you need to submit graded work.