forked from tpemartin/110-1-r4ds-main
-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.1 4.2
168 lines (131 loc) · 4.1 KB
/
4.1 4.2
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
---
title: "ch4"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
```{r}
typeof(c("John", "Mary"))
typeof(c(2, 3.1412))
typeof(c(TRUE, TRUE, F))
```
```{r}
class(c("John", "Mary"))
class(c(2, 3.1412))
class(c(TRUE, TRUE, F))
```
```{r}
datetimeinfo<-"2021-01-01 12:03:33"
```
```{r}
install.packages("lubridate")
```
```{r}
datetimeiofo2<-lubridate::ymd_hms("2021-01-01 12:03:33") + lubridate::seconds(20)
class(datetimeiofo2)
```
```{r}
commonClasses <- list()
```
```{r}
# save three different atomic vectors
commonClasses$character <- c("John", "Mary", "Bill")
commonClasses$numeric <- c(2.3, 4, 7)
commonClasses$logical <- c(TRUE, T, F, FALSE)
```
```{r}
# check each atomic vector class
class(commonClasses$character) # name call on commonClasses to get its value then retrieve the element value whose element name is "character"
class(commonClasses$numeric)
class(commonClasses$logical)
```
```{r}
bloodTypes <- c("AB", "AB", "A", "B", "A", "A", "B", "O", "O", "AB")
```
```{r}
bloodTypes_fct <-
factor(bloodTypes)
bloodTypes_fct
```
```{r}
table(bloodTypes_fct)
```
```{r}
levels(bloodTypes_fct)
```
```{r}
bloodTypes_fct_levelsSetup <-
factor(bloodTypes, levels=c("A", "B", "O", "AB"))
bloodTypes_fct_levelsSetup
```
```{r}
class(bloodTypes_fct_levelsSetup)
```
```{r}
table(bloodTypes_fct_levelsSetup)
```
```{r}
household_income <- c("low income", "low income", "middle income", "low income", "high income", "middle income", "high income", "high income", "middle income", "middle income")
```
```{r}
household_income_fct <-
factor(household_income)
levels(household_income_fct)
```
```{r}
household_income_fct_levelsSetup <-
factor(household_income, levels = c("low income", "middle income", "high income"))
levels(household_income_fct_levelsSetup)
```
```{r}
table(household_income_fct_levelsSetup)
```
```{r}
household_income_fct_levelsSetup[[1]]
household_income_fct_levelsSetup[[1]] > "low income"
```
```{r}
household_income_fct_levelsSetup_ordered <-
factor(
household_income,
levels = c("low income", "middle income", "high income"),
ordered = T
)
household_income_fct_levelsSetup_ordered
```
```{r}
household_income_fct_levelsSetup_ordered[[1]]
household_income_fct_levelsSetup_ordered[[1]] > "low income"
household_income_fct_levelsSetup_ordered[[3]]
household_income_fct_levelsSetup_ordered[[3]] > "low income"
```
```{r}
commonClasses$ordered_factor <- household_income_fct_levelsSetup_ordered
class(commonClasses$ordered_factor)
```
```{r}
#exercise 4.1
customerExperience <- c('very happy','very happy','satisfied','satisfied','satisfied','very happy','bad','bad','satisfied','satisfied','bad','happy','happy','very happy','happy','happy','satisfied','very happy','very happy','satisfied','satisfied','very happy','satisfied','bad','very happy','very bad','very happy','bad','bad','very bad')
customerExperience_fct <-factor(customerExperience)
table(customerExperience_fct)
levels = c("very bad","bad","satisfied ","happy","very happy")
customerExperience_fct_ordered<-
factor(customerExperience_fct,
levels = c("very bad","bad","satisfied ","happy","very happy"),
ordered = T )
customerExperience_fct_ordered[[1]]<"happy"
customerExperience_fct_ordered[[1]]>"happy"
```