-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathR_Lab_Exercise_1.Rmd
More file actions
312 lines (220 loc) · 4.86 KB
/
Copy pathR_Lab_Exercise_1.Rmd
File metadata and controls
312 lines (220 loc) · 4.86 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
---
title: "DS311 - Basic R Lab Exercise"
author: "Norman Lo"
date: "10/25/2024"
output:
pdf_document: default
html_document: default
subtitle: R Lab Exercise
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Basic R Exercise
## Section 1 - Data Type
**Key Functions**
- typeof()
- as.numeric()
- as.charater()
### Numeric
```{r}
# Numeric - Double precision by default
n1 <- 15
n1
typeof(n1)
n2 <- 1.5
n2
typeof(n2)
```
### Character
```{r}
# Character
c1 <- "c"
c1
typeof(c1)
c2 <- "a string of text"
c2
typeof(c2)
```
### Logical
```{r}
# Logical
l1 <- TRUE
l1
typeof(l1)
l2 <- F
l2
typeof(l2)
```
### Transforming Numerics and Characters
``` {r}
# Transforming numeric into characters
num <- 10
numToChar <- as.character(num)
paste("num Type: ", typeof(num), " | numToChar: ", typeof(numToChar))
# Transforming characters into numeric
char <- "10"
charToNum <- as.numeric(char)
paste("char Type: ", typeof(char), " | charToNum: ", typeof(charToNum))
```
### Challenge:
Complete the following tasks:
``` {r}
# Check the data type of the following variables
a <- as.integer(500)
b <- as.double(500)
c <- as.character(500)
# Enter your code here!
is.double(b)
is.integer(a)
is.character(c)
# Check the data type of the new variable 'd'
d <- a / b
# Enter your code here!
is.character(d)
is.double(d)
```
***
## Section 2 - Data Structure
- is.vector()
- is.matrix
- cbind()
- as.data.frame()
### Vector
```{r}
# Vector
v1 <- c(1, 2, 3, 4, 5)
v1
is.vector(v1)
v2 <- c("a", "b", "c")
v2
is.vector(v2)
v3 <- c(TRUE, TRUE, FALSE, FALSE, TRUE)
v3
is.vector(v3)
```
### Matrix
```{r}
# Matrix
m1 <- matrix(c(T, T, F, F, T, F), nrow = 2)
m1
is.matrix(m1)
m2 <- matrix(c("a", "b",
"c", "d"),
nrow = 2,
byrow = T)
m2
is.matrix(m2)
```
### Challenge:
1. Create a vector of the 26 alphabet lower case letters in sequence.
2. Create a 2 by 13 matrix for the 26 English upper case letter in sequence.
Hint: Check out the "letters" and "LETTERS" key words in R.
```{r}
# Enter your code here.
lower_alpha <- letters
upper_alpha <- LETTERS
lower_alpha
upper_alpha
upper_mat <- matrix(LETTERS,nrow = 2, ncol = 13, byrow = TRUE)
upper_mat
```
### DataFrame
```{r}
# Data Frame
# Can combine vectors of the same length
vNumeric <- c(1, 2, 3)
vCharacter <- c("a", "b", "c")
vLogical <- c(T, F, T)
df1 <- cbind(vNumeric, vCharacter, vLogical)
df1 # Coerces all values to most basic data type
df2 <- as.data.frame(cbind(vNumeric, vCharacter, vLogical))
df2 # Makes a data frame with three different data types
```
***
## Section 3 - Setup Working Directory and Installing Packages
**Key Functions:**
- getwd()
- setwd()
- install.packages()
- library()
### Setting up your working directory
``` {r}
# Check your current working directory
# wd1 <- getwd()
# paste("Current Working Directory: ", wd1)
# Setting the working directory for a project
# setwd("c://.../project")
# wd2 <- getwd()
# paste("Current Working Directory: ", wd2)
```
### Installing and Loading Packages
```{r, include=FALSE}
# Install a new package, note the quotation marks
# install.packages("mass")
# Install multiple packages at once
# install.packages(c("dplyr", "ggplot2"))
# Loading the package, note no quotation marks
# library(dplyr)
# Checking the package version
# packageVersion("dplyr")
# List all functions in a package
# ls("package:ggplot2")
# Loading a function from package
# ggplot2::geom_line
# Update all packages
# update.packages()
# Unload a package
# detach(package:ggplot2, unload=TRUE)
# Help function
# help(dplyr)
# Checking the session info
# sessionInfo()
```
***
## Section 4 - Problem Solving
Write the code that accomplish the following tasks:
Part a: Assign 4 to variable x
x <- 4
Part b: Assign 12 to variable y
y <- 12
Part c: Print both x and y to check their values
print(x)
print(y)
Part d: Divide y by x and assign it to variable z
part e: Print a statement to report your answer in Part d.
Once you finished and knit the RMarkdown file into html file, you should be able to see the message "Congratulation!! You completed the first exercise in this section!!" in the html document.
print("Congratulation!! You completed the first exercise in this section!!")
```{r}
# Write your code here!
# Part a
x <- 4
# Part b
y <- 12
# Part c
z <- y / x
print(x)
print(y)
# Part d
x = 2
y = 3
z = 10
z <- y / x
# Part e
print(paste("y divided by x is equal to ", z))
print("Congratulation!! You completed the first exercise in this section!!")
x = 4
y = 12
z = 3
z <- y / x
# Do not need to change the following code!
if (exists("x") == TRUE | exists("y") == TRUE | exists("z") == TRUE){
if (x == 4 & y == 12 & z == 3) {
print("Congratulation!! You completed the first activity in this class!!")
} else {
print("Sorry, you got it wrong!")
}
} else {
print("You did not complete the last problem!")
}
```