## Questions

Exercise: 5-A

Write a function called “multiply” that accepts two numbers as arguments and outputs the product of those two numbers when called as is demonstrated below.

``````multiply(3, 3)
# [1] 9``````
Exercise: 5-B

Write an equation that returns the remainder of 12 divided by 8.

Exercise: 5-C

Write an equation that returns the remainder of 36 divided by 10.

Exercise: 5-D

Write a “while” loop that prints all even numbers from 0 to 10.

It’s possible for this task to be accomplished in several ways; however, the output of your program should always look like this:

``````# [1] 0
# [1] 2
# [1] 4
# [1] 6
# [1] 8
# [1] 10``````
Exercise: 5-E

You are given a vector that looks like this:

``numbers <- c(0:12)``

Write a for loop that loops through your vector and prints any element greater than or equal to 3.

It’s possible for this task to be accomplished in several ways; however, the output of your program should always look like this:

``````# [1] 3
# [1] 4
# [1] 5
# [1] 6
# [1] 7
# [1] 8
# [1] 9
# [1] 10
# [1] 11
# [1] 12``````
Exercise: 6-A

Convert the following character variable to a variable with the data type “raw”:

``x <- "Trevor rocks"``

You should store your raw data in a variable named “raw_data”, print the data to the console, and check the data type with the “typeof” function. Your output should look like the following:

``````print(raw_data)
# [1] 54 72 65 76 6f 72 20 72 6f 63 6b 73
typeof(raw_data)
# [1] "raw"``````
Exercise: 6-B

Create a variable named “spending” and give it a value of 120. Then create a variable named “budget” and give it a value of 100. Next, check whether spending is greater than budget and store the resulting logical data in a variable named “over_budget”. Finally, print the value of “over_budget” variable and check it’s data type with the “typeof” function.

Your final output should look like this:

``````print(over_budget)
# [1] TRUE
typeof(over_budget)
# [1] "logical"``````
Exercise: 7-A

Create a vector named “animal” and give it the following three values: “cow”, “cat”, “pig”. Create a second vector named “sound” and give it the following three values: “moo”, “meow”, “oink”. Finally, create a data frame named “animal_sounds” and assign each of these vectors to be a column.

After printing the resulting data frame to the console, you should get the following output:

``````#   animal sound
# 1    cow   moo
# 2    cat  meow
# 3    pig  oink``````

## Answers

Answer: 5-A

One way you could accomplish this task is demonstrated in the following solution.

``````multiply <- function(x, y) {
return (x * y)
}

multiply(3, 3)``````
``[1] 9``
Answer: 5-B

A remainder is referred to as “modulus” in programming. We can use the “%%” operator to accomplish this. For this example, the output of your equation should be 4.

``12 %% 8``
``[1] 4``
Answer: 5-C

A remainder is referred to as “modulus” in programming. We can use the “%%” operator to accomplish this. For this example, the output of your equation should be 6.

``36 %% 10``
``[1] 6``
Answer: 5-D

Here’s one way you could write your `while` loop to achieve this output:

``````i <- 0

while (i <= 10) {
print(i)
i <- i + 2
}``````
``````[1] 0
[1] 2
[1] 4
[1] 6
[1] 8
[1] 10``````
Answer: 5-E

Here’s one way you could write your `for` loop to achieve this output:

``````numbers <- c(0:12)

for (number in numbers) {
if (number >= 3) {
print(number)
}
}``````
``````[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10
[1] 11
[1] 12``````
Answer: 6-A

You can accomplish this task with the “charToRaw” function.

``````x <- "Trevor rocks"
raw_data <- charToRaw(x)
print(raw_data)``````
`` [1] 54 72 65 76 6f 72 20 72 6f 63 6b 73``
``typeof(raw_data)``
``[1] "raw"``
Answer: 6-B

The following example demonstrates how you can accomplish this task.

``````spending <- 120
budget <- 100
over_budget <- spending > budget
print(over_budget)``````
``[1] TRUE``
``typeof(over_budget)``
``[1] "logical"``
Answer: 7-A

The following example demonstrates how you can accomplish this task.

``````animal <- c("cow", "cat", "pig")
sound <- c("moo", "meow", "oink")
animal_sounds <- data.frame(animal = animal, sound = sound)
print(animal_sounds)``````
``````  animal sound
1    cow   moo
2    cat  meow
3    pig  oink``````