# rmarkdown::run("variables.Rmd")
library(learnr)
gradethis::gradethis_setup()

Variables

This code computes the answer to one plus one, change it so it computes two plus two:

1 + 1

Attribute to x the value 2.5, and compute the exponential of its squared value:

x <- ___
___
x <- 2.5
exp(x^2)
grade_code()

Attribute the string "100.3" to the variable x, and the value 99.6 to the variable y:

x <- ___
y <- ___
x <- "100.3"
y <- 99.6
grade_code()

Assuming the x and y defined above, modify the following code so that it does not return an error:

x <- "100.3"
y <- 99.6
floor(x) - ceiling(y)
# look into as.numeric()
floor(as.numeric(x)) - ceiling(y)
grade_code()

Booleans

I defined two random variables x and y. Are the two variables equal? which one is smaller?

x <- runif(1)
y <- runif(1)

x == y
grade_code()

I defined two random variables x and y. Using the ifelse(){.R} function, print "x is larger than y" or "x is smaller than y".

x <- runif(1)
y <- runif(1)
ifelse(test, yes, no)
ifelse(x < y, "x is smaller than y", "x is larger than y")
grade_code()

Without running the code, what do you think will be the output of this code:

cos(pi/2)==cos(3*pi/2)
cos(pi/2)-cos(3*pi/2)

In fact, .Machine$double.eps is the smallest positive floating-point number x such that 1 + x != 1. It depends on the machine (computer) you are running on:

.Machine$double.eps
(cos(pi / 2) - cos(3 * pi / 2)) > .Machine$double.eps
all.equal(cos(pi/2),cos(3*pi/2))

Strings

Save your name as a string in a variable, and print "My name is: yourname" using this variable.

name <- ___
print(___, ___)
name <- "___"
name <- "Colin"
paste("My name is:", name)
grade_code()

Replace all "e" by "A" in the following string:

x <- "aaaeebbbeebiieelakdceee"
gsub(___, ___, ___)
x <- "aaaeebbbeebiieelakdceee"
gsub("e", "A", x)
grade_code()

Do the same using the stringr function str_replace_all(){.R} (take a look at the cheatsheet):

library(stringr)
x <- "aaaeebbbeebiieelakdceee"
str_replace_all(___, ___, ___)
library(stringr)
x <- "aaaeebbbeebiieelakdceee"
str_replace_all(x, "e", "A")
grade_code()

Find the index of all the "e" characters in the string, using base functions and stringr ones (take a look at the cheatsheet to find the good one):

x <- "aaaeebbbeebiieelakdceee"
gregexpr(___, ___)
library(stringr)
str____(___, ___)
x <- "aaaeebbbeebiieelakdceee"
gregexpr("e", x)
library(stringr)
str_locate_all(x, "e")
grade_code()


colinbousige/tutor documentation built on Jan. 29, 2023, 7:35 p.m.