Lab 2 Solution

$30.00 $24.00

You will submit an HTML document created using R Notebook, but you are more than welcome to test your code out in an R script first. BE SURE TO SAVE YOUR WORK REGULARLY!!! R as a calculator Perform the following calculations in R. Be careful about order of operations; you may have to perform some…

5/5 – (2 votes)

You’ll get a: zip file solution

 

Categorys:
Tags:

Description

5/5 – (2 votes)

You will submit an HTML document created using R Notebook, but you are more than welcome to test your code out in an R script first.

BE SURE TO SAVE YOUR WORK REGULARLY!!!

R as a calculator

Perform the following calculations in R. Be careful about order of operations; you may have to perform some tests to make sure R is behaving the way you expect.

  1. e3

  1. log(100) ## use base 10

  1. ln(4) ## use the natural log

  1. 11 – 4/5

  1. 510/5

  1. 72+5

  1. 7 ## can you think of two ways to compute this?

Vector Practice

Create vectors with the following specifications:

  1. All integers from 1 to 100

  1. The odd integers between 1 and 99

  1. The numbers 1, 2, 3, 1, 2, 3, 1, 2, 3 etc. [1, 2, 3 repeated 10 times], using the rep() function

  1. The numbers 1, 1, 1, 2, 2, 2, 3, 3, 3, . . . , 50, 50, 50, using rep()

  1. The numbers 1, 2, 3, . . . , 50, 49, 48, 47, . . . , 1, 0

Without just typing all of the numbers in yourself, create:

  1. The fractions 1, 1/2, 1/3, 1/4, 1/5, 1/6, 1/7, 1/8, 1/9, 1/10

  1. The numbers 1, 8, 27, 64, 125, 216, . . . , 1000

Using the seq() function, create:

  1. A sequence of 350 equally spaced numbers from 0 to 0.5

  1. A sequence of 100 numbers starting at 1 and spaced by 0.25 Now, create and compute the following:

  2. A vector called age with the following ages of people: 17, 19, 16, 34, 65, 72, 47, 52, 10, 12

  1. Display the value of the 4th element in the vector (the age of the 4th subject)

1

  1. Display the 1st and 10th elements

  1. Display all but the 5th element

  1. Change the 5th age (65) to 56

  1. Create a Boolean (TRUE/FALSE) vector indicating which subjects are over the age of 50. Name this vector “over.50”

  1. Create a vector “child” that indicates which subjects are under the age of 18. That is, children should have the value “TRUE” for this vector and adults should have the value “FALSE”

  1. Display the ages of the children

  1. Display the indices of subjects who are 34 years old

  1. Type sum(age > 34). What’s going on here?

  1. Count the number of children using the “child” variable

  1. Use the length() function to display the age of the last subject

Matrix Practice

  1. Create a matrix M as follows M <- matrix(1:16, nrow = 4)

  2. Display the element in the 4th row, 3rd column

  1. Display the 3rd row of M

  1. Display the first, second, and fourth columns of M

  1. Use the which() command to display the row and column indices of all elements of M that have the value of 2

  1. Create a Boolean matrix, B, indicating which elements of M are greater than 2

  1. Write a line of code to count the number of elements in M that are greater than 2

  1. Create the following matrix, and name it M2. Display M2.

1

5

9

13

2 6 10 14

3

7

11

15

4

8

12

16

37) Create the 4 by 4 identity matrix, and name it I. Display I.

1000

0100

0

0

1

0

0

0

0

1

  1. Use the which() command to display the row and column indices for the elements of I that are equal to 1

  1. Multiple the matrix I times M2 using the matrix multiplication operator, %*%.

  1. Now perform the operation I * M2. What happens? This is not standard matrix multiplication, but can be a useful operation to perform.

2

  1. Compute M2 + I and M2 – I

  1. The R command to compute the transpose of a matrix is t(). Use this command to compute the transpose of M2.

  1. Use the diag function to display the diagonal of M2.

  1. Use the solve() function to find the inverse of M2. Name the inverse M2.inv. Display M2.inv.

  1. Multiply M2 times its inverse (using standard matrix multiplication). Do you get the identity? Note that the inverse is not computed with perfect precision, so the product will not have perfect precision, either. That is, some elements of the product may be slightly different from 0 or slightly different from 1.

  1. Round the product that you compute in 45) to 5 decimal places.

3

Lab 2 Solution
$30.00 $24.00