-
Notifications
You must be signed in to change notification settings - Fork 1
/
Loop vs Apply.R
246 lines (175 loc) · 4.65 KB
/
Loop vs Apply.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
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
#Let's look at 'for' loops first!
# Create a vector filled with random normal values
u1 <- rnorm(30)
print("This loop calculates the square of the first 10 elements of vector u1")
# Initialize `u squared`
usq <- 0
for(i in 1:10) {
# i-th element of `u1` squared into `i`-th position of `u squared`
usq[i] <- u1[i]*u1[i]
print(usq[i])
}
#now lets try a nested for loop
## Create a 30 x 30 matrix (of 30 rows and 30 columns)
mymat <- matrix(nrow=30, ncol=30)
# For each row and for each column, assign values based on position:
#product of two indexes
for(i in 1:dim(mymat)[1]) {
for(j in 1:dim(mymat)[2]) {
mymat[i,j] = i*j
}
}
# Just show the upper left 10x10 chunk
mymat[1:10, 1:10]
#Do i always have to use i??
#No!
## Create a 30 x 30 matrix (of 30 rows and 30 columns)
mymat <- matrix(nrow=30, ncol=30)
# For each row and for each column, assign values based on position:
#product of two indexes
for(z in 1:dim(mymat)[1]) {
for(p in 1:dim(mymat)[2]) {
mymat[z,p] = z*p
}
}
# Just show the upper left 10x10 chunk
mymat[1:10, 1:10]
#Let's do a 3 dimensional array for...fun
# Create your three-dimensional array
my_array <- array(1:20, dim=c(20, 20, 20))
for (i in 1:dim(my_array)[1]) {
for (j in 1:dim(my_array)[2]) {
for (k in 1:dim(my_array)[3]) {
my_array[i,j,k] = i*j*k
}
}
}
# Show a 10x10x15 chunk of your array
my_array[1:10, 1:10, 1:15]
#Now let's go onto 'while' loops
# Your User Defined Function
readinteger <- function()
{ n <- readline(prompt="Please, enter your ANSWER: ")
}
response<-as.integer(readinteger());
#type any number besides 42 into the console now, then run the rest of the code
while (response!=42)
{
print("Sorry, the answer to whatever the question MUST be 42");
response<-as.integer(readinteger());
}
#now let's do a repeat loop
readinteger <- function(){
n <- readline(prompt="Please, enter your ANSWER: ")
}
repeat {
response <- as.integer(readinteger());
if (response == 42) {
print("Well done!");
break
} else print("Sorry, the answer to whatever the question MUST be 42");
}
#adding breaks to loops
# Make a lower triangular matrix (zeroes in upper right corner)
m=10
n=10
# A counter to count the assignment
ctr=0
# Create a 10 x 10 matrix with zeroes
mymat = matrix(0,m,n)
for(i in 1:m) {
for(j in 1:n) {
if(i==j) {
break;
} else {
# you assign the values only when i<>j
mymat[i,j] = i*j
ctr=ctr+1
}
}
print(mymat)
}
# Print how many matrix cells were assigned
print(ctr)
#example using 'next'
#this code says if a number has a non-zero remainder when divided by 2,
#print it, if not, skip it.
m=20
for (k in 1:m){
if (!k %% 2)
next
print(k)
}
#Vectorization
#you could add a series of numbers together using a loop
a <- 1:10
b <- 1:10
res <- numeric(length = length(a))
for (i in seq_along(a)) {
res[i] <- a[i] + b[i]
}
res
#why isn't this OK?
#lets look at the ruin time for a bigger example
a <- 1:10000000
b <- 1:10000000
res <- numeric(length = length(a))
system.time(for (i in seq_along(a)) {
res[i] <- a[i] + b[i]
})
res
"Or we could use the vectorized function '+'"
a <- 1:10
b <- 1:10
res2<-a+b
res2
#lets see how much faster this is for our bigger numbers
a <- 1:10000000
b <- 1:10000000
system.time(res2<-a+b)
#we can sometimes perform operations to vectors of unequal length
a <- 1:10
b <- 1:5
a + b
#this seems weird until we se another example
a <- 1:10
b <- 5
a * b
#what if the longer vector is not a multiple of the shorter vector?
a <- 1:10
b <- 1:7
a + b
#lets look at the apply functions
data = matrix(rnorm(20), nrow=5, ncol=4)
# row sums
apply(data, 1, sum)
# row means
apply(data, 1, mean)
# col sums
apply(data, 2, sum)
# col means
apply(data, 2, mean)
#Let's do lapply now
data = list(l1 = 1:10, l2 = 1000:1020)
lapply(data, mean)
#sapply
data = list(l1 = 1:10, l2 = runif(10), l3 = rnorm(10,2))
lapply(data, mean)
sapply(data, mean)
#tapply
data = c(1:10, rnorm(10,2), runif(10))
groups = gl(3,10)
tapply(data, groups, mean)
#One more tapply example (data,groups,mean)
data(iris)
tapply(iris$Sepal.Length, iris$Species, mean)
#mapply
list(rep(1,4), rep(2,3), rep(3,2), rep(4,1))
mapply(rep, 1:4, 4:1)
#Let's do one real-world 'apply' example
data("mtcars")
mtcars
#2 means that this is applied column wise, "1" would apply is row wise
# 1 would be more closely associated with a for loop
apply(mtcars,1,mean)
apply(mtcars,2,function(x) sin(x))