29/12/2017 Introduction to R Software - - Unit 4 - Week 3 - Conditional executions and loops, data management with sequences.
Unit 4 - Week 3 - Conditional executions and loops, data
management with sequences.
Assignment 3
.
1) If x = 123 then which one of the following is the correct outcome of 1 point
y=if ( x==33 ) { x <- x^2 } else { x <- x^3 }?
1860867
15129
123
None of these
Accepted Answers:
1860867
2) Which one of the following is the correct syntax to compute y for the following function? 1 point
if ( x <= 0 ) { y <- 2 * x^2 + abs(x - 6) } else { y <- 3 * x^3 -
sqrt(x^5 }
if ( x <= 0 ) ( y <- 2 * x^2 + abs(x - 6) ) else ( y <- 3 * x^3 -
sqrt(x^5))
if ( x <= 0 ) ( y < 2 * x^2 + abs(x - 6) ) else (x > 0) ( y < 3 * x^3
)
if ( x >= 0 ) { y <- 2 * x^2 } else (x > 0) { y <- 3 * x^3 - sqrt(x^5)
}
Accepted Answers:
if ( x <= 0 ) ( y <- 2 * x^2 + abs(x - 6) ) else ( y <- 3 * x^3 - sqrt(x^5))
3) Suppose x = c(2,4,6,8,10) then which one of the following is the correct output of 1 point
ifelse(x<6,2*x,10*x)?
20 40 12 16 20
20 40 60 80 100
4 8 12 16 20
4 8 60 80 100
[Link] 1/8
29/12/2017 Introduction to R Software - - Unit 4 - Week 3 - Conditional executions and loops, data management with sequences.
Accepted Answers:
4 8 60 80 100
4) Which one of the following is the correct outcome of for(i in 2:4) {print(1 + 2i)}? 1 point
5 9 7
[1] 1
[1] 2i
[1] 1+2i
[1] 1+2i
[1] 1+2i
[1] 1+2i
Error...
Accepted Answers:
[1] 1+2i
[1] 1+2i
[1] 1+2i
5) Which one of the following is the correct outcome of for(i in 20:24){print(i+2*i)}? 1 point
[1] i+2i
[1] i+2i
[1] i+2i
[1] i+2i
[1] i+2i
[1] 60
[1] 63
[1] 66
[1] 69
[1] 72
Error...
None of these
Accepted Answers:
[1] 60
[1] 63
[1] 66
[Link]
29/12/2017 Introduction to R Software - - Unit 4 - Week 3 - Conditional executions and loops, data management with sequences.
[1] 69
[1] 72
6) Which one of the following is the correct outcome of the following commands about while 1 point
loop?
z <- 3
while (z < 15) {
print(c("R Course","is helpful."))
z = z + 2
print(c("R Course","is not helpful."))
z = z + 1
}
[1] "R Course" "is not helpful."
[1] "R Course" "is helpful."
[1] "R Course" "is not helpful."
[1] "R Course" "is helpful."
[1] "R Course" "is not helpful."
[1] "R Course" "is helpful."
[1] "R Course" "is not helpful."
[1] "R Course" "is helpful."
[1] "R Course" "is helpful." "R Course" "is not helpful."
"R Course" "is helpful." "R Course" "is not helpful." "R
Course" "is helpful." "R Course" "is not helpful." "R
Course" "is helpful." "R Course" "is not helpful."
[1] "R Course" "is helpful."
[1] "R Course" "is not helpful."
[1] "R Course" "is helpful."
[1] "R Course" "is not helpful."
[1] "R Course" "is helpful."
[1] "R Course" "is not helpful."
[1] "R Course" "is helpful."
[1] "R Course" "is not helpful."
None of these
Accepted Answers:
[1] "R Course" "is helpful."
[1] "R Course" "is not helpful."
[1] "R Course" "is helpful."
[1] "R Course" "is not helpful."
[Link] 3/8
29/12/2017 Introduction to R Software - - Unit 4 - Week 3 - Conditional executions and loops, data management with sequences.
[1] "R Course" "is helpful."
[1] "R Course" "is not helpful."
[1] "R Course" "is helpful."
[1] "R Course" "is not helpful."
7) Which one of the following is the correct outcome of the following while loop? 1 point
x <- 10
while(x < 50) {x <- x+10; if (x == 40) break; print(x^2);}
[1] 400
[1] 900
[1] 1600
[1] 2500
[1] 20
[1] 30
[1] 20
[1] 30
[1] 40
[1] 50
Accepted Answers:
[1] 400
[1] 900
8) Which one of the following is the correct outcome of the command abs(seq(-2,2))? 1 point
2 1 0 1 2
0 1 2
-2 -1 0 1 2
None of these
Accepted Answers:
2 1 0 1 2
9) Which one of the following is the correct output of the command sqrt(abs(seq(-6,6, 1 point
by = 3)))?
0.000000 1.732051 2.449490
2.449490 1.732051 0.000000 1.732051 2.449490
- 2.449490 -1.732051 0.000000 1.732051 2.449490
None of these
Accepted Answers:
2.449490 1.732051 0.000000 1.732051 2.449490
10)Which one of the following is the correct output of the command seq(18,-18,by=-5)? 1 point
[Link] 4/8
29/12/2017 Introduction to R Software - - Unit 4 - Week 3 - Conditional executions and loops, data management with sequences.
-17 -12 -7 -2 3 8 13 18
-17 -12 -7 -3 3 7 12 17
17 12 7 3 -3 -7 -12 -17
18 13 8 3 -2 -7 -12 -17
Accepted Answers:
18 13 8 3 -2 -7 -12 -17
11)Which one of the following is the correct output of the command seq(to = 100, length 1 point
= 8)?
30 40 50 60 70 80 90 100
93 94 95 96 97 98 99 100
100 99 98 97 96 95 94 93
101 102 103 104 105 106 107 108
Accepted Answers:
93 94 95 96 97 98 99 100
12)Which one of the following is the correct output of the command seq(to = -20, 1 point
length = 10)?
-29 -28 -27 -26 -25 -24 -23 -22 -21 -20
-20 -21 -22 -23 -24 -25 -26 -27 -28 -29
20 21 22 23 24 25 26 27 28 29
None of these
Accepted Answers:
-29 -28 -27 -26 -25 -24 -23 -22 -21 -20
13)Which one of the following is the correct output of the command seq(to = 22, length 1 point
= 5, by = 6)?
2 4 10 16 22
22 16 10 4 2
-2 4 10 16 22
None of these
Accepted Answers:
-2 4 10 16 22
14)Which one of the following is the correct output of the command seq(from = -10, 1 point
length = 5, by = -0.3)?
-11.2 -10.9 -10.6 -10.3 -10.0
-10.0 -10.3 -10.6 -10.9 -11.2
10.0 10.3 10.6 10.9 11.2
11.2 10.9 10.6 10.3 10.0
Accepted Answers:
-10.0 -10.3 -10.6 -10.9 -11.2
[Link] 5/8
29/12/2017 Introduction to R Software - - Unit 4 - Week 3 - Conditional executions and loops, data management with sequences.
15)Which one of the following is the correct output of X for the following commands 1 point
Y <- c(90, 77, 51, 20, 30, 60, 89)
X <- seq(along = Y)?
1 2 3 4 5 6 7
7 5 3 1 2 4 6
7 6 5 4 3 2 1
None of these
Accepted Answers:
1 2 3 4 5 6 7
16)Which one of the following is the correct output of Y[X[2]] and Y[X[6]] for the command 1 point
Y <- c(900, 717, 251, 280, 302, 670, 897)
X <- seq(along = Y)?
3 and 4 respectively.
4 and 3 respectively.
670 and 717 respectively.
717 and 670 respectively.
Accepted Answers:
717 and 670 respectively.
17)Which one of the following is the correct command to generate a sequence of three yearly 1 point
dates 1-Jan-2015, 1-Jan-2016 and 1-Jan-2017?
seq([Link]("2015-01-01"), [Link]("2017-01-01"), by = "years")
seq([Link]("2015-01-01"), [Link]("2016-01-01"), [Link]("2017-01-
01"),by = "years")
seq([Link](`2015-01-01’), [Link](`2017-01-01’), by = `1’)
seq([Link]("2015-01-01"), [Link]("2017-01-01"), by = "years")
Accepted Answers:
seq([Link]("2015-01-01"), [Link]("2017-01-01"), by = "years")
18)Which one of the following is the correct command to generate the dates of 9 consecutive 1 point
months starting from 5 Jan 2017?
seq([Link]("2017-01-05"), by = "months", length = 9)
seq([Link]("2017-01-05"), by = "months", length = 9)
seq([Link]("2017-01-05"), by = "months", length = 9)
seq([Link]("2017-01-05") to [Link]("2017-09-05") by = "9")
Accepted Answers:
seq([Link]("2017-01-05"), by = "months", length = 9)
19)Which one of the following is the correct outcome of the command letter[1:3]? 1 point
"a" "b" "c"
"A" "B" "C"
"4" "5"
Error...
[Link] 6/8
29/12/2017 Introduction to R Software - - Unit 4 - Week 3 - Conditional executions and loops, data management with sequences.
Accepted Answers:
Error...
20)Which one of the following is the correct outcome of the command LETTERS[18:12]? 1 point
"r" "q" "p" "o" "n" "m" "l"
"R" "Q" "P" "O" "N" "M" "L"
"L" "M" "N" "O" "P" "Q" "R"
Error...
Accepted Answers:
"R" "Q" "P" "O" "N" "M" "L"
21)Which one of the following is the correct outcome of the command rep(2:5,4)? 1 point
2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5
2 3 3 4 4 4 5 5 5 5
2 3 4 5 2 3 4 5 2 3 4 5 2 3 4 5
2:5 2:5 2:5 2:5
Accepted Answers:
2 3 4 5 2 3 4 5 2 3 4 5 2 3 4 5
22)Which one of the following is the correct outcome of the command rep(70:65, 1 point
times=3)?
70 69 68 67 66 65 70 69 68 67 66 65 70 69 68 67 66 65
65 66 67 68 69 70 65 66 67 68 69 70 65 66 67 68 69 70
70 69 68 67 66 65 65 66 67 68 69 70 70 69 68 67 66 65
65 66 67 68 69 70 70 69 68 67 66 65 65 66 67 68 69 70
Accepted Answers:
70 69 68 67 66 65 70 69 68 67 66 65 70 69 68 67 66 65
23)Which one of the following is the correct outcome of the command rep(20:25, each=3)? 1 point
20 21 22 23 24 25 20 21 22 23 24 25 20 21 22 23 24 25
20 20 20 21 21 21 22 22 22 23 23 23 24 24 24 25 25 25
20:25 20:25 20:25
25 25 25 24 24 24 23 23 23 20 20 20 22 22 22 21 21 21
Accepted Answers:
20 20 20 21 21 21 22 22 22 23 23 23 24 24 24 25 25 25
24)Which one of the following is the correct outcome of the commands 1 point
x <- matrix(nrow=2, ncol=2, data=1:4, byrow=T)
rep(x, each=3)?
1 1 1 4 4 4 3 3 3 2 2 2
1 1 1 3 3 3 2 2 2 4 4 4
1 2 3 4 1 2 3 4 1 2 3 4
None of these
[Link] 7/8
29/12/2017 Introduction to R Software - - Unit 4 - Week 3 - Conditional executions and loops, data management with sequences.
Accepted Answers:
1 1 1 3 3 3 2 2 2 4 4 4
25)Which one of the following is the correct outcome of the command rep(c("name1", 1 point
"name2", "name3"), each=2)?
"name1" "name1" "name2" "name2" "name3" "name3"
"name1" "name2" "name3" "name1" "name2" "name3"
name 1 2 3 name 1 2 3
name 1 1 name 2 2 name 3 3
Accepted Answers:
"name1" "name1" "name2" "name2" "name3" "name3"
[Link] 8/8