-
Notifications
You must be signed in to change notification settings - Fork 1
/
exercise7.Rmd
134 lines (98 loc) · 2.66 KB
/
exercise7.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
## 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.
<br>Remember you can comment the code using #.
<details>
<summary>
*Answer*
</summary>
```{r, eval=F}
getwd()
setwd("Rcourse/Module2")
setwd("~/Rcourse/Module2")
```
</details>
**1- Install and load packages ggplot2 and WriteXLS**
<details>
<summary>
*Answer*
</summary>
```{r, eval=F}
# 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. Was versions of the packages did you get?
</details>
**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?
<details>
<summary>
*Answer*
</summary>
```{r, eval=F}
# Dimensions of diamonds
dim(diamonds)
# Column names of diamonds
colnames(diamonds)
```
</details>
You can read the help page of the diamonds dataset to understand what it contains!<br>
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.**
<details>
<summary>
*Answer*
</summary>
```{r, eval=F}
# Select columns
diams1 <- diamonds[,c("carat", "cut", "color", "price")]
```
</details>
**4- Install and load the package dplyr from the Console.**
<details>
<summary>
*Answer*
</summary>
```{r, eval=F}
# Install package
install.packages(pkgs="dplyr")
# Load package
library("dplyr")
```
</details>
**5- Use the function "sample_n" from the dplyr package to randomly sample 200 lines of diams1: save in diams object.**
<details>
<summary>
*Answer*
</summary>
```{r, eval=F}
# Subset data frame
diams <- sample_n(tbl=diams1, size=200)
```
</details>
**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.
<details>
<summary>
*Answer*
</summary>
```{r, eval=F}
# Write a text file with write.table
write.table(x=diams,
file="diamonds200.txt",
row.names=FALSE,
quote=FALSE,
sep="\t")
# Write an Excel file with WriteXLS
WriteXLS(x=diams,
ExcelFileName="diamonds200.xls",
row.names=FALSE,
col.names=TRUE,
FreezeRow=1,
BoldHeaderRow=TRUE)
```
</details>