Let's dive in R with an iconic programming example:
Write a simple script to say hello to the world. To do that, first create a variable called greetings and assign it the value Hello world!. Then, print greetings to the screen with the function:
print()
Answer
greetings <- 'Hello world!'
print(greetings)
print is not a complicated function, but reading documentation is a good programming habit. Let's check the documentation available for print. To do so, type:
?print
and look at the left panel in RStudio. Try to answer the following questions:
- How many named arguments does print have?
- How do you control if strings should be print with or without quotes?
Answers
- 10
- By setting the argument 'quote' to TRUE if quotes are desired and FALSE otherwise.
An easy way to check the type of a given variable is by using the function
class()
Now define the following variables and set them to the values below
a <- 10
b <- FALSE
c <- "Hola mundo"
d <- 10.00
and check the class of all of them.
Answer
- numeric
- logical
- character
- numeric
Write the following code
a <- 10.3333
b <- as.integer(a)
Now answer the following questions:
- What is the type of a?
- What is the type of b?
- What's the difference between the values of a and b. Why?
- What is as.integer doing?
Answers
- Numeric
- Integer
- A is a numeric type variable whose value is a decimal number.
- as.integer is converting a numeric variable into an integer. Since an integer cannot have a fractional component, it is discarded.
Write a script that:
- Calculates and stores the result of: 126 * 37
- Checks If the result of 126 * 37 is greater than 4660
Answer
result <- 126 * 37
comparison <- (result > 4660)
print(comparison)
Write a script that divides 1600 by 1250 and retrieves the remainder of the operation. Then, check if the remainder is less than 30. Print the result.
Answer
remainder <- 1600 %% 1250
comparison <- reminder < 30
print(comparison)
We can use runif()
to generate random numbers. For instance, to generate a
random rational number between 1 and 20, we can type:
a <- runif(min=1, max=20, n=1)
Write a script that uses the aforementioned function to generate three numbers, from 1 to 100, and save them to three variables called first, second and third respectively.
Now check if first is greater than or equal to second. Store the result to a variable called first_vs_second. Print its value.
After that, divide second by third and multiply it by first. Store and print the result.
Answer
first <- runif(min=1, max=100, n=1)
second <- runif(min=1, max=100, n=1)
third <- runif(min=1, max=100, n=1)
first_vs_second <- first >= second
print(first_vs_second)
result <- second / third * first
print(result)
Define in a script the following variables:
a <- 10
b <- 15
d <- 2
Then, write a script that performs the following arithmethic operations:
- Sum a and b
- Divide the result by d
- Print the result
You cannot write more than one line of code for both arithmethic operations.
Answer
a <- 10
b <- 15
d <- 2
result <- (a + b) / d # One line of code
print(result)
Remember: In programming, arithmethic operators work the same as their mathemathic counterparts. That means that, unless you are using parenthesis, division is performed before sum.