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 #.
1- Install and load the packages ggplot2 and WriteXLS
correction
# Install the 2 packages at once
install.packages(pkgs=c("ggplot2", "WriteXLS"))
# Load in the environment (one by one)
library("ggplot2")
library("WriteXLS")
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?
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.
4- Install and load the package dplyr from the Console.
5- Use the function “sample_n” from the dplyr package to randomly sample 200 lines of diams1: save in diams object.
-6.Save diams into 2 files:
- diamonds200.txt with write.table
- diamonds200.xls with WriteXLS
Note: read about and play with the different options of both functions and check the output files.