-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotes.R
209 lines (133 loc) · 3.27 KB
/
notes.R
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
## Ideas to cover
# square brackets
# round brackets
# dollar signs
# assigning
# colon
# lists
# arrays
# data frames
# matrices
#
## Functions to cover
# ls
# rm
# read.csv
# write.csv
# summary
# table
# unique
# attributes
# plot
# hist
# is.numeric
# is.array
# is.data.frame
# is.matrix
# apply
# tapply
# lapply
# paste
# Sys.time
# dim
# do.call
# names
# %in%
# ==, >, <, |, &
# which
x <- rnorm(10)
x > 0
which(x == 0)
which(x < 0)
new_data <- x[x > 0]
x <- c(x, NA)
y <- x[!is.na(x)]
(x+1)[!is.na(x+1) & (x+1 > 0)] -> z
v1 <- x + 1;
v2 <- v1[!is.na(v1)];
v3 <- v2[v2 > 0];
#or (better)
x[!is.na(x) & (x>0)] + 1 -> z2
#system.time tells you how long it takes for an operation to run.
system.time(x[!is.na(x) & (x>0)] + 1 -> z2)
#Get values 1 through 10
x[1:10]
#or 10 through 1
x[10:1]
#Get values of x except values 1 through 10
x[-(1:10)]
fruit <- c(5,10,1,20)
names(fruit) <- c("orange","banana","apple","peach")
lunch <- fruit[c("apple","orange")]
dinner <- fruit[c("banana","peach")]
#Replace NA with zero
x[is.na(x)] <- 0
x
# Note
abs(y)
#Is the same as:
y[y < 0] <- -y[y < 0]
#Matrices and Multidimensional Arrays
#Matrices have two indices A = a[i,j]
#Data frames have any nmber of indices A = a[i1,i2,i3...]
#commands include: array(), matrix(), is.matrix(), data.frame(), as.data.frame(), etc.
#Factor is an object type. How we handle categorical variables; has levels. A list is a collection of objects of any sort. A function is an object which given objects returns something; we'll be making these.
#Vectors are of numeric values, real or complex. They will answer to the question is.numeric() and is.vector()
is.numeric(x)
is.vector(x)
#A list is a recursive structure. (other recursive structures that act on atomic structures). Other recursive structures include functions and expressions.
#An expression...
#There is a function called mode(object) and one called length(object)
mode(x)
length(x)
attributes(x)
attributes(fruit)
#Note:
x
as.character(x)
as.numeric(as.character(x))
e <- NULL
mode(e)
e <- c(1,e)
mode(e)
#Mode changes, now e is a vector of just 1.
e <- numeric()
mode(e)
e
e[3] <- 17
e
#Note we get a vector of three, first two elements are NA, the third is 17.
alpha <- 1:10/10
#Take every second element
alpha[2 * (1:5)] -> alpha
#To truncate alpha at three:
length(alpha) <- 3
u <- 1:100
attributes(u)
attr(u,"dim") <- c(10,10)
#This turns the dimensions of u to a 10 X 10 matrix.
#Note something very confusing in R: The way R handles matrices is to fill things in column-wise first, not row-wise. So it will go down column [1,], then start filling column [2,], etc... To get around this:
u2 <- matrix(data = u, ncol=10, nrow=10, byrow=TRUE)
#Transposing works too.
tu <- t(u)
#But not with more than two dimensions.
m <- 1:24
attr(m,"dim") <- c(2,3,4)
#Note:
m[1,1,1]
m[2,1,1]
m[1,2,1]
m[2,2,1]
#The fastest changing index is the first. Fill that, then go to the next layer.
m1 <- array(data = 1:24, dim = c(2,3,4))
variable1 <- matrix(1:20, ncol=5,nrow=4,byrow=FALSE)
variable1
variable1[2,]
#Even rows:
variable1[2*(1:2)-1,]
rownames(variable1) <- c("a","b","c","d")
variable1["c",]
colnames(variable1) <- c("32","34","36","38","40")
variable2 <- as.data.frame(variable1)
is.data.frame(variable2)
variable2[,c(1,4,5)]