Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Introduction to Data Science Methods

Week 2 Lab

STATS 2DA3

Basic Functions

•  Use the sum(), mean(), and prod() functions in R to obtain the following for the vector vec = c(1, 2, 3 , 4) ;

• Calculate the sum and print out the words“Sum of the vector:”

• Calculate the mean and print out the words “Mean of the vector:”

 Calculate the product and print out the words “Product of the vector:”

Basic Functions

• Type ?mean into the R console and read the documentation, then complete the following tasks for the vector vec = c(1.1, NA, 2, 3.0, NA);

• Try to calculate the sum and print out the words“Sum of the vector:” What happens?

• Try to calculate the mean including the NA values and print out the words“Mean of the vector with NA values:”What happens?

• Calculate the mean excluding the NA values and print out the words “Mean of the vector without NA values:”What happens?

• Similarly, try to calculate the product of the vector, including and then excluding the NA values. What do you observe?

Vectors

•  Use the ? function in R to get help on a topic, e.g. type ?class into the R console and read about the function class .

• Create and print a vector called CAR, containing the elements“red”, “green”, and“yellow”

  Print the class of the vector.

Factors

• Create a vector called colours  CAR containing the following elements: ‘green’,‘green’,‘yellow’,‘red’,‘red’,‘red’,‘green’

• Create a factor object, i.e. make colours  CAR into a factor with levels.

• Ask R to print out how many levels there are in your factor.

Matrices

Write code to produce the following matrix. Write the code in 2 diferent ways, i.e. filling by row, and then create the matrix a second time, this time lling it by column.