Exercises 3 - Common Data Objects

Files corresponding to Short Course: Introduction to Data Science Using R

Exercises 3 - Common Data Objects

3.1 Vectors

  1. Create a vector vec with elements 2, 4, 6, …, 12.

  2. Write code to see the 1st, 3rd, and 5th elements of vec in the console.

  3. You can exclude elements from a vector by using a ‘-’ sign in front of their indices. Utilize the length() function to see the vec object in the console without its last element.

3.2 Subsetting data frames

  1. The iris dataframe is a built-in R object. Write code to show the 4th column of the iris dataset in the console (there are many ways to do this!).

  2. Write code to see just the first five rows and just the Species and Sepal.Width columns of the iris object in the console.

  3. Write code to see iris dataset in the console with the columns displayed in alphabetical order. This can be done using the sort() function. Use the result of sort(colnames(iris)) along with [] to select the columns in alphabetical order.