-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise_10_3.R
50 lines (42 loc) · 1.06 KB
/
exercise_10_3.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
#Question A
loopvec1 <- 5:7
loopvec2 <- 9:6
foo <- matrix(NA, length(loopvec1), length(loopvec2))
for(i in 1:length(loopvec1)) {
foo[i,] <- loopvec1[i] * loopvec2
}
#Question B
chars <- c("Peter","Homer","Lois","Stewie","Maggie","Bart")
num_vals <- rep(NA, times=length(chars))
for (i in 1:length(chars)) {
num_vals[i] <- switch(EXPR=chars[i],Homer=12,Marge=34,Bart=56,Lisa=78,Maggie=90,NA)
}
#Question C
#i
mylist <- 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=list(c("hello", "you"), matrix(c("hello", "there"))),
ff=matrix(c("red", "green", "blue", "yellow"))
)
#ii
mylist <- list("tricked you", as.vector(matrix(1:6, 3, 2)))
#iii
mylist <- list(
list(1,2,3), list(c(3,2), 2),
list(c(1, 2), matrix(c(1,2))),
rbind(1:10, 100:91)
)
matrix_count <- 0
for (member in mylist) {
if (is.matrix(member)) {
matrix_count <- matrix_count + 1
} else if (is.list(member)) {
for (submember in member) {
if (is.matrix(submember)) {
matrix_count <- matrix_count + 1
}
}
}
}