-
Notifications
You must be signed in to change notification settings - Fork 0
/
for-loops.R
69 lines (59 loc) · 1.46 KB
/
for-loops.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
#An example of a for loop
for (myitem in 5:7) {
cat("--BRACED AREA BEGINS--\n")
cat("the current item is", myitem, "\n")
cat("--BRACED AREA ENDS--\n\n")
}
#Manipulate objects outside of a loop
counter <- 0
for (myitem in 5:7) {
counter <- counter + 1
cat("The item in run", counter, "is", myitem, "\n")
}
#Looping via index vs. looping via value
myvec <- c(0.4, 1.1, 0.34, 0.55)
for (i in myvec) {
print(2*i)
}
for (i in 1:length(myvec)) {
print(2*myvec[i])
}
#Inspect members of a list using a loop
foo <- list(
aa=c(3.4, 1), bb=matrix(1:4, 2, 2),
cc=matrix(c(T, T, F, T, F, F), 3, 2), dd="string here",
ee=matrix(c("red", "green", "blue", "yellow"))
)
name <- names(foo)
is.mat <- rep(NA, length(foo))
nr <- is.mat
nc <- is.mat
data.type <- is.mat
for (i in 1:length(foo)) {
member <- foo[[i]]
if(is.matrix(member)) {
is.mat[i] <- "Yes"
nr[i] <- nrow(member)
nc[i] <- ncol(member)
data.type[i] <- class(as.vector(member))
} else {
is.mat[i] <- "No"
}
}
bar <- data.frame(name, is.mat, nr, nc, data.type, stringsAsFactors = FALSE)
#Nesting for loops
loopvec1 <- 5:7
loopvec2 <- 9:6
foo <- matrix(NA, length(loopvec1), length(loopvec2))
for(i in 1:length(loopvec1)) {
for(j in 1:length(loopvec2)){
foo[i, j] <- loopvec1[i]*loopvec2[j]
}
}
#Match inner and outer loop indexes
foo <- matrix(NA, length(loopvec1), length(loopvec2))
for (i in 1:length(loopvec1)) {
for (j in 1:i) {
foo[i, j] <- loopvec1[i]+loopvec2[j]
}
}