forked from rstudio/Intro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomework2PartB_ElliotTruslow.R
More file actions
162 lines (122 loc) · 2.02 KB
/
Homework2PartB_ElliotTruslow.R
File metadata and controls
162 lines (122 loc) · 2.02 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
#Part 1B
#Code for Assignment
#setting directory to run code from repo without errors
setwd("~/Desktop/Homework/Intro/")
getwd()
# NOTE: removed 2 lines that were throwing errors in the starting code.
## |---------| Part B |---------|
### 01-Reshape.R
## 01-Structures.R
# Code from 01-structures
5 + 5
4 - 1
1 * 2
4 ^ 2
a <- 1
b <- 2
a + b
A <- 3
a + b - A
round(3.1415)
factorial(3)
sqrt(9)
factorial(round(2.0015) + 1)
# Your Turn
# ---------
10 + 2
12 * 3
36 - 6
30 / 3
# ---------
# R objects
foo <- 42
foo <- round(3.1415) + 1
foo
factorial(foo)
foo
rm(foo)
foo
pi
pi <- 1
pi
rm(pi)
pi
# Data structures
# Warm up
# ---------
WorldPhones
# ---------
vec <- c(1, 2, 3, 10, 100)
vec
mat <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2)
mat
matrix(c(1, 2, 3, 4, 5, 6), nrow = 2)
matrix(c(1, 2, 3, 4, 5, 6), nrow = 3)
vec + 4
vec * 4
vec * vec # element-wise multiplication
vec %*% vec # matrix multiplication (inner)
vec %o% vec # matrix multiplication (outer)
mat
t(mat) # transpose
array(c(1, 2, 3, 4, 5, 6), dim = c(2, 2, 3))
# Data types
1 + 1
3000000
class(0.00001)
"hello"
class("hello")
"hello world"
nchar("hello")
paste("hello", "world")
# Warm up
# ---------
1
"1"
"one"
# ---------
3 > 4
class(TRUE)
class(T)
fac <- factor(c("a", "b", "c"))
fac
class(fac)
# Quiz
# ---------------------
x <- c(1, 2, 3)
x
"x"
# ---------------------
# Your Turn
# ---------------------
vec <- c(1, "R", TRUE)
class(vec)
vec
# ---------------------
# Quiz
# ---------
c(5, "two")
c(TRUE, "a")
c(1, "TRUE")
TRUE + 5
# ---------
as.numeric("1")
as.character(TRUE)
lst <- list(1, "R", TRUE)
class(lst)
list(c(1, 2), TRUE, c("a", "b", "c"))
df <- data.frame(c(1, 2, 3),
c("R","S","T"), c(TRUE, FALSE, TRUE))
class(df)
nvec <- c(one = 1, two = 2, three = 3)
nvec
nlst <- list(one = 1, two = 2, many = c(3, 4, 5))
nlst
ndf <- data.frame(numbers = c(1, 2, 3),
letters = c("R","S","T"),
logic = c(TRUE, FALSE, TRUE))
ndf
names(ndf)
names(nvec)
names(nvec) <- c("uno", "dos", "tres")
nvec