To get started with this assignment, run:

bio185::startAssignment("01-r-101")

That will create a file named 01-r-101.Rmd in your current working directory and update any data files you need in your data/ directory.

R 101 review

Let’s start simple and review the use of variables and doing basic math operations.

Save the number 10 in a variable named a and then print its contents:

## [1] 10

Store 2 in the variable b and print it:

## [1] 2

Add a to b:

## [1] 12

Subtract b from a:

## [1] 8

Multiply a and b:

## [1] 20

Divide a by b (surprise, surprise):

## [1] 5

Raise a to the power of b:

## [1] 100

Strings

Store the strings “Hello” and “world” in a character vector assigned to the variable welcome and then evaluate welcome:

## [1] "Hello" "world"

Find the length of your welcome variable:

## [1] 2

Practice Indexing

Create a vector holding the numbers 1 through 42 and save it in a variable called x. Then print it.

##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## [24] 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

Extract the numbers in this vector at positions 10 and 20 (in the same operation):

## [1] 10 20

Extract numbers 10 through 20:

##  [1] 10 11 12 13 14 15 16 17 18 19 20

Create a vector containing the remainder when each number in x is divided by 2:

##  [1] 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
## [36] 0 1 0 1 0 1 0

Create a logical vector which is TRUE for even numbers and FALSE for odd numbers in x:

##  [1] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
## [12]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE
## [23] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
## [34]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE

Now, put it all together to extract the even numbers from x:

##  [1]  2  4  6  8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42

Replace all of the odd numbers in the original x vector with the missing value flags (NA), then print x.

##  [1] NA  2 NA  4 NA  6 NA  8 NA 10 NA 12 NA 14 NA 16 NA 18 NA 20 NA 22 NA
## [24] 24 NA 26 NA 28 NA 30 NA 32 NA 34 NA 36 NA 38 NA 40 NA 42