## 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)
#  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:

``````#  0
#  2
#  4
#  6
#  8
#  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:

``````#  3
#  4
#  5
#  6
#  7
#  8
#  9
#  10
#  11
#  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)
#  54 72 65 76 6f 72 20 72 6f 63 6b 73
typeof(raw_data)
#  "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)
#  TRUE
typeof(over_budget)
#  "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``````

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

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

multiply(3, 3)``````
`` 9``

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``
`` 4``

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``
`` 6``

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

``````i <- 0

while (i <= 10) {
print(i)
i <- i + 2
}``````
`````` 0
 2
 4
 6
 8
 10``````

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)
}
}``````
`````` 3
 4
 5
 6
 7
 8
 9
 10
 11
 12``````

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

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

The following example demonstrates how you can accomplish this task.

``````spending <- 120
budget <- 100
over_budget <- spending > budget
print(over_budget)``````
`` TRUE``
``typeof(over_budget)``
`` "logical"``
``````animal <- c("cow", "cat", "pig")
``````  animal sound