-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4_2.R
More file actions
56 lines (49 loc) · 999 Bytes
/
4_2.R
File metadata and controls
56 lines (49 loc) · 999 Bytes
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
#' ###
#' APTS 4. "Some exercises"
#' ###
rm(list=ls())
# Question 2 --------------------------------------------------------------
# a) ----
X <- matrix(runif(100000), 1000, 100)
z <- rep(0, 1000)
system.time(
for(i in 1:1000){
for(j in 1:100){
z[i] <- z[i] + X[i, j]
}
}
) # This takes 0.03 seconds
X[1:5, 1:5]; z[1:5]
z.loop <- z
# First using apply
system.time(
z.apply <- apply(X, 1, sum)
)
# And then using rowSums
system.time(
z.rowsums <- rowSums(X)
)
which(z.loop != z.apply)
which(z.loop != z.rowsums)
# Both faster and give same results.
# b) ----
rm(list=ls())
n <- 100000
z <- rnorm(n)
zneg <- 0; j <- 1
system.time(
for(i in 1:n){
if(z[i] < 0){
zneg[j] <- z[i]
j <- j + 1
}
}
) # This takese 0.04 seconds and gives 50,146 negative values.
zneg.loop <- zneg
zneg <- c()
system.time(
zneg <- unlist(lapply(z, function(x) if(x < 0) x))
) # This is a bit slower, oops.
system.time(
zneg <- subset(z, z < 0)
) # okay this is better!