-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplad8310_2_objects.Rmd
More file actions
265 lines (191 loc) · 5.87 KB
/
plad8310_2_objects.Rmd
File metadata and controls
265 lines (191 loc) · 5.87 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
---
title: "PLAD8310_2_objects"
author: "Zach Johnson"
date: "`r Sys.Date()`"
output:
pdf_document: default
word_document: default
html_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
### **Introduction to R Objects and Help Documentation**
Today, we'll learn about **objects** in R and how to effectively read and interpret help documentation. This skill is critical for understanding and applying new functions or packages you’ll encounter in your work.
---
### **1. What Are Objects in R?**
In R, an **object** is anything you store in the Environment. Common types of objects include:
- **Numbers** or **strings** (e.g., `"Zach"` or `42`)
- **Vectors** (e.g., `c(1, 2, 3)`)
- **Matrices** (tables of values)
- **Data frames** (rows = observations, columns = variables)
- **Functions**, **models**, or **graphs**
Objects store your data so you can manipulate, analyze, and visualize it. These are the building blocks of statistical computing! You will be working with these in every project you do, so it is important to memorize keys aspects of object usage and manipulation.
---
### **2. Assigning Values to Objects**
Assign values to objects using `<-`. Let's look at a simple example:
```{r}
library(ggplot2)
```
```{r}
# Creating an object
my_name <- "Zach" # Assigns a string
my_name
```
```{r}
my_name <- 83113 # Overwrites the value with a number
my_name
```
Note that `my_name` completely forgot its previous value (`"Zach"`). Once you overwrite an object, this is irreversible and you must recode it.
#### **Removing an Object**
You can delete an object using the `remove()` function:
```{r}
remove(my_name)
```
---
### **3. Using Help Documentation**
When working with a new function or package, reading the help documentation is essential. Use `?` or `help()` to pull up a function's documentation. We are going to do this throughout the entirety of the course. Let’s practice:
#### **How to Read Help Files:**
- **Description:** A brief overview of what the function does.
- **Usage:** How to write the function in your code.
- **Arguments:** Inputs the function accepts.
- **Examples:** Code snippets showing how to use the function.
---
### **4. Vectors**
Vectors are the most basic data structure in R. All elements in a vector must be of the same type (e.g., numbers or text).
#### Example:
```{r}
# Creating vectors
char_vector <- c("R", "is", "fun")
num_vector <- c(10, 20, 30)
```
```{r}
# Performing operations on vectors
num_vector + 5 # Adds 5 to each element
```
#### **Using Help with Vectors:**
Try pulling up the documentation for `c()`, which creates vectors:
---
### **5. Matrices**
A matrix is a two-dimensional table of values. All elements in a matrix must also be of the same type.
#### Example:
```{r}
# Creating a matrix
my_matrix <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3)
my_matrix
```
#### **Useful Functions for Matrices:**
- `is.matrix()` to check if an object is a matrix.
- `as.matrix()` to convert an object into a matrix.
```{r}
is.matrix(my_matrix)
as.matrix(my_matrix)
```
#### **Matrix Algebra in R:**
```{r}
# Define matrices
A <- matrix(c(-1, 3, 4), nrow = 3, ncol = 1)
B <- matrix(c(3, -2, 1), nrow = 3, ncol = 1)
C <- matrix(c(3, 2, -4, -8, 0, 6), nrow = 2, ncol = 3, byrow = TRUE)
D <- matrix(c(6, -2, -1, 3, -3, 8), nrow = 2, ncol = 3, byrow = TRUE)
# Display matrices
A
B
C
D
```
Matrix Addition and Scalar Multiplication: Compute 7A + 3B
```{r}
#Note that R follows order of operations
result_1 <- 7 * A + 3 * B
result_1a <- (7*A) + (3*B)
result_1
result_1a
```
Inner (Dot) Product
```{r}
inner_product <- C %*% D
inner_product
```
Outer (Cross) Product
```{r}
# Outer product
outer_product <- outer(A,B)
outer_product
```
Matrix Multiplication
```{r}
result_2 <- C %*% A
result_2
```
Inverses
```{r}
# Define matrix
M <- matrix(c(2, 1, 7, 4), nrow = 2, ncol = 2)
# Solve for the inverse
inverse_M <- solve(M)
inverse_M
```
Determinant
```{r}
# Determinant
det_M <- det(M)
det_M
```
---
### **6. Data Frames**
Data frames are like matrices but can mix types (e.g., numbers and text). Each column is a variable, and each row is an observation.
#### Example:
```{r}
# View the built-in `cars` dataset
View(cars)
```
#### **Explore the Help for Data Frames:**
---
### **7. Using Models and Plots as Objects**
You can store regression models and plots as objects for reuse:
```{r}
# Creating a model object
lm_model <- lm(dist ~ speed, data = cars)
# Creating a plot object
scatter_plot <- ggplot(cars, aes(speed, dist)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE)
scatter_plot
```
#### **Help for `lm()`**
---
### **8. Additional Content: Lists and Their Versatility**
A **list** can hold objects of different types (e.g., vectors, matrices, and data frames).
#### Example:
```{r}
# Creating a list
my_list <- list(
name = "Zach",
scores = c(90, 85, 78),
matrix_data = matrix(1:4, nrow = 2)
)
# Accessing elements of a list
my_list$name
my_list$scores
my_list$matrix_data
```
---
### **9. Exploring and Modifying Objects**
#### **Check Object Type:**
Use `class()` to check an object’s type:
```{r}
class(my_matrix)
class(my_list)
```
#### **Structure of an Object:**
Use `str()` to understand its structure:
```{r}
str(my_list)
```
#### **Help for `class()` and `str()`:**
---
### **10. Wrap-Up: Key Takeaways**
- **Objects:** Everything you work with in R is an object.
- **Help Documentation:** Always check documentation (`?`) for functions or packages to understand their usage.
- **Practice:** Assign, manipulate, and explore objects to deepen your understanding.