12.8 Exercise 7: Library and packages

Create the script “exercise7.R” and save it to the “Rcourse/Module2” directory: you will save all the commands of exercise 7 in that script.
Remember you can comment the code using #.

correction

1- Install and load the packages ggplot2 and WriteXLS

correction

Check with sessionInfo() that the packages were loaded.

2- ggplot2 loads automatically the diamonds dataset in the working environment: you can use it as an object after ggplot2 is loaded.

What are the dimensions of diamonds? What are the column names of diamond?

correction

You can read the help page of the diamonds dataset to understand what it contains!

Note: diamonds is a data frame: you can test it with is.data.frame(diamonds) (returns TRUE).

3- Select the columns carat, cut, color and price of diamonds and store in the object diams1.

correction

4- Install and load the package dplyr from the Console.

correction

5- Use the function “sample_n” from the dplyr package to randomly sample 200 lines of diams1: save in diams object.

correction

6- Save diams into 2 files (different file formats):

  • diamonds200.txt with function write.table (from package utils)
  • diamonds200.xls with function WriteXLS (from package of the same name WriteXLS).

Note: read about and play with the different options of both functions and check the output files.

correction