2) Multiply each value in this matrix by 7 and store it in a 10 x 10 matrix

mat <- matrix(1:100, nrow = 10, ncol = 10)

   

3 a) Print these values as part of a string that looks something like 'n = 16'.

Try to describe in words how I creating the vector of numbers that you are going to use.

set.seed(1)
x <- round(runif(min = 10, max = 100, n = 15))

You should get something like this:

## [1] "n = 34"
## [1] "n = 43"
## [1] "n = 62"
## [1] "n = 92"
## [1] "n = 28"
## [1] "n = 91"
## [1] "n = 95"
## [1] "n = 69"
## [1] "n = 67"
## [1] "n = 16"
## [1] "n = 29"
## [1] "n = 26"
## [1] "n = 72"
## [1] "n = 45"
## [1] "n = 79"

   

b) Now modify this loop to store these strings in a new vector called counts.

   

4) Make a vector for which each entry is 2 raised to the power of it’s index (ex: the 3rd item in the vector is equal to 2^3).

For example your loop should return a vector that looks something like this:
2, 4, 8, 16, 64, …, 1024

for (i in 1:10) {
    2 ^ i
}

   

5) Make a matrix where each entry, using indexes i for row and j for column, is equivalent to i*j. Your final output should look like:

##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
##  [1,]    1    2    3    4    5    6    7    8    9    10    11    12
##  [2,]    2    4    6    8   10   12   14   16   18    20    22    24
##  [3,]    3    6    9   12   15   18   21   24   27    30    33    36
##  [4,]    4    8   12   16   20   24   28   32   36    40    44    48
##  [5,]    5   10   15   20   25   30   35   40   45    50    55    60
##  [6,]    6   12   18   24   30   36   42   48   54    60    66    72
##  [7,]    7   14   21   28   35   42   49   56   63    70    77    84
##  [8,]    8   16   24   32   40   48   56   64   72    80    88    96
##  [9,]    9   18   27   36   45   54   63   72   81    90    99   108
## [10,]   10   20   30   40   50   60   70   80   90   100   110   120
## [11,]   11   22   33   44   55   66   77   88   99   110   121   132
## [12,]   12   24   36   48   60   72   84   96  108   120   132   144

   


Including conditional statements


A conditional statement is an if this, do that. In programming, you’ll hear people talk about if statements, or if/else statements: if this then that else do something different.

Before you begin, if you have not used if/else statements before checkout at least this first link to get you going. This first link is nice and bare bones, getting to the how to right away.

If you would like more to read, I often remind myself of how to write if/else statements from these:

   

6) Make a vector where each entry is TRUE or FALSE, based on whether it’s index is even or odd.

x <- 1:10

   

7)

Run this code to set yourself up for question 7.

taxa <- c('Coral', 'fish', 'Fish', 'Phytoplankton', 'coral', 'phytoplankton', 
          'zooplankton', 'Zooplankton', 'Echinoderms', 'echinoderms', 
          'Cephalopods', 'cephalopods')

taxa_values <- sample(taxa, size = 100, replace = TRUE)
set.seed(1)
counts <- round(runif(min = 10, max = 500, n = 100))

taxa_counts <- data.frame(taxa = taxa_values, abundance = counts)

   

a) Using dplyr, calculate the mean abundance of each taxonomic group, what do you notice about the output? Is it what you would expect?

   

b) Hopefully not. What’s going on? Can you fix it?

If you’re struggling to figure out what to do, think about how you would go about solving the problem in excel, this might help you figure out what you should try and google. (otherwise I’ll give you a hint about a useful function to use here).