You are on page 1of 11

Final Exam Fall2016 #0000001280 | On January 1, 2017 21:52

Email deneme@ufuk.com

Score Obtained 43 out of 100 (43.00%)

Administrator Remarks Processing

StudentID 2099111000

Email deneme@ufuk.com

What is stored in z[2,3]? 23


z <- matrix(,3,3)
z[c(1:2), c(2:3)] <- matrix(c(20,21,22,23), nrow=2) NA
z <- z[z[,2] >= 5,]
22

21

Which of the following can be used to print only 3 to the console? my.list[[3]][[1]][[1]][[1]][[1]]
my.list <- list(vec1 = c(1,2), vec2 = c(3,4),list(list(list(list(3),
c(1:2))))) my.list[[3]][[1]][[1]]

my.list[[2]]

my.list[[1]][[3]][[1]][[1]]

I have a numeric vector. Which one of the following functions can be used to obtain the ii
average of the numbers in the vector?
i. str i,ii
ii. summary
i
iii. factor
i, ii, iii

Which of the following does the increment (by 10) and also ensures that nobody will have a
final score higher than 100? finals[finals > midterms] <-
The instructor of cmpe 140 course is impressed by the performance of students such that finals[finals > midterms] + 10
he/she wants to add extra 10 points to the final scores of everyone whose final score is finals[finals > 100] <- 100
higher than his/her midterm score. Final and midterm scores are stored in the vectors named
“finals” and “midterms”.
finals[finals > midterms,] <-
ifelse(finals - midterms < 0, +10, 0)
finals[finals > 100,] <- 100

finals[finals >> midterms] <-


finals[finals >> midterms] + 10
finals[finals >> 100] <- 100

finals[,finals > midterms] <-


finals[,finals > midterms] + 10
finals[,finals > 100] <- 100

Which values does "my.data" contain?


"small" "1" "small" "5" "huge" "9" "huge"
my.vector <- c(1,5,9,13)
"13"
my.data <- c()
for(i in my.vector){
if(i <= 5){ "small" 1 "small" 5 "huge" 9 "huge" 13
my.data <- c(my.data, "small")
}else{
my.data <- c(my.data, "huge") 1 "small" 5 "small" 9 "huge" 13 "huge"
}
my.data <- c(my.data, i)
"1" "small" "5" "small" "9" "huge" "13" "huge"
}

Which one of the codes below would be the best choice? hist(random1000,breaks=seq(-4,4,0.25))
The code above is executed in R. I want this data to look more accurate, in the sense that I
want to observe more data points in the histogram. Which one of the codes below would be hist(random1000,breaks=seq(-4,4,1))
the best choice?
hist(random1000,breaks=c(-4,4,0.25))
random1000 <- rnorm(1000)
hist(random1000) hist(random1000,breaks=c(-4,4,1))
Which of the following is false?
The ages of 13 children in a neighborhood are assigned to a vector named “ages”:
ages <- c(7,8,8,9,10,12,13,14,15,15,16,16,17)
Here is the boxplot that summarizes the data: The bold line segment corresponds to 12.3
since it is the mean of the data set.

If there was another child in the set with age 13,


the boxplot would change.

The bottom of the box corresponds to 9 which


splits off the lowest 25% of the data from the
highest 75%.

The top of the box corresponds to 15 which splits


off the lowest 75% of the data from the highest
25%.

What is the output if the following code script is executed?


(hint: %% is the modulo operator in R. This operation finds the remainder after division of one
number by another. For example, 5%%2 is 1. ) 14
func <-function(n){
i <- 1
sum <- 0
while(i<n){
15
sum <- sum +i
i <- i+1
}
repeat{
if(sum%%2==0){
10
break
}
sum <- sum-1
}
return(sum) 20
}
func(6)

What is the correct output if the following code script is executed?


myfunction<-function(vector){ 6 3 5 12 10
i <- 1
count <- 0
while(i<=length(vector)){
if(i > vector[i]){ 6 3 5 12 4 10
vector[i] <- 0
}
i <- i+1 6 0 3 5 0 12 0 10
}
vector <- vector[vector>0]
return(vector)
} 6 0 3 5 0 12 10
myfunction(c(6,-1,3,5,-4,12,4,10))

What is the output of the code above?


z <- 0 [1] 2
while(z < 5) { [1] 4
z <- z + 2 [1] 6
print(z)
} [1] 2,4,6

[1] 2,3,4,5,6

[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
Which of the following data sets would yield the box plot below?

c(2,2,4,6,6,6,8,8)

c(2,3,4,6,6,6,7,8)

c(2,2,4,5,6,7,8,8)

c(2,3,6,6,6,6,7,8)

What is the output?


surprise <- function(v1){ 836
result <- c()
for(i in length(v1):1){
638
result <- c(result,v1[i])
}
return(result)
472
}

x <- surprise(1:3) + surprise(c(1,5)) Error


x

How to obtain the following chart?


slices <- c(10, 12, 4, 16, 8)
lbls <- c("US", "UK", "Australia",
"Germany", "France")
pct <- round(slices/sum(slices)*100)
lbls <- paste(lbls, pct)
lbls <- paste(lbls,"%",sep="")
pie(slices,labels = lbls,
col=rainbow(length(lbls)),main="Countr
ies")

slices <- c(10, 12, 4, 16, 8)


lbls <- c("US", "UK", "Australia",
"Germany", "France")
lbls <- paste(lbls, c(20,24,8,32,16))
lbls <- paste(lbls,"%",sep="")
plot(slices,labels = lbls,
col=rainbow(length(lbls)),main="Countr
ies")

slices <- c(10, 12, 4, 16, 8)


lbls <- c("US", "UK", "Australia",
"Germany", "France")
pct <- round(slices/sum(slices)*100)
lbls <- paste(pct,lbls)
lbls <- paste(lbls,"%",sep="")
pie(slices,labels = lbls,
col=rainbow(5),main="Countries")

slices <- c(10, 12, 4, 16, 8)


lbls <- c("US", "UK", "Australia",
"Germany", "France")
lbls <- paste(c(20,24,8,32,16),lbls)
lbls <- paste(lbls,"%",sep=" ")
pie(slices,labels = lbls,
col=rainbow(length(lbls)),main="Countr
ies")
What is the output?
label_me <- function(vec,def="th"){
for (i in 1:length(vec)) { 1st 2nd 3rd NA NA
name <- "" 1 30 40 12 11
label <- ""
if(i == 1){
label <- "1st"
} else if(i == 2){ 1st 2nd 3rd 4th 5th
label <- "2nd" 1 30 40 12 11
} else if(i == 3){
label <- "3rd"
} else {
label <- paste(i,def,sep="")
break 1st 2nd 3rd 4th NA
} 1 30 40 12 11
names(vec)[i] <- label
}
print(vec)
} 1st 2nd 3rd
1 30 40
label_me(c(1,30,40,12,11))

In which steps did Smith make a mistake?


Smith has data which contain the students’ names and grades. I
Name MT1 MT2
Keynes 65 60
Friedman 80 95
Marx 55 75 II
Smith has to find students who are eligible to take the final exam and store them as a
Boolean entry in Eligible column. In order to take the final exam one has to have an average
above 65.
In order to find these students Smith follows these steps :
I. Smith creates a matrix “Grade” which stores the data and has columns “Name”,”MT1″ and I, II, III
“MT2” respectively.
II. He adds a column “Avg” which holds the average grades of students by using
Grade$Avg <- (Grade[[2]]+Grade[[3]])/2
III. Then He adds a column “Eligible” by using All steps are correct.
Grade$Eligible <- ifelse(Grade[[4]]>65,T,F)

Which of the following ones will do the job?


data.txt is as follows:
names homework midterm final attendance
I, V
1 Elif 95 80 87 TRUE
2 Can 60 62 100 FALSE
3 Cem 40 38 62 FALSE
4 Deniz 92 85 85 FALSE
5 Selin 100 74 72 TRUE
6 Efe 25 56 61 TRUE
7 Ezgi 85 78 87 TRUE I, II
8 Ali 94 67 98 TRUE
x <- read.table("data.txt",header=T)
We have a data frame called as x. We read the data above from a file named input.txt into a
data frame named x and we want to give a new form in which attendance and homework are
replaced. Which of the following ones will do the job?
I. x <- x[order(c(1,5,3,4,2))]
III, IV
II. x <- x[(1,5,3,4,2)]

III.
x <- x[2]<x[5]
x <- x[5]<-x[2]
II, IV
IV. x <- x[order(1,5,3,4,2)]

V. x <- x[c(1,5,3,4,2)]

Which of the following codes should you run?


Your friend sends you a set of data as a text file (named abc.txt) for a project you two are read.table("abc.txt", header = T, sep = "-")
working on.
Here is how the set of data begins:
Variable-Distribution-Mean table("abc.txt", sep = ",")
X-Normal-6.54
Y-Poisson-4.5
Z-Uniform-6
..... read(abc.txt, header=F, sep = "")
You would normally create a new data frame with this information by typing yourself.
However, you see that the list goes on and on and you do not have enough time. After
making sure that the text file is located in your working directory, which of the following read.table(header = T, abc.txt, sep = "-")
codes should you run?

Which command finds the grand average of all numbers? mean(sapply(lapply(newlist,mean),sum))


A list is formed as follows:
newlist <- list(c(1,2,3),c(4,5,6)) lapply(newlist,mean)

mean(lapply(newlist,mean))

sapply(newlist,mean)
Which one(s) is/are true?
We have a list called mylist. I
mylist <- list(v1=c(1,2), v2=c(3,4), v3=c(5,6))
I.
mylist[[3]]
[1] 5 6 I-II-III-IV
II.
mylist[[“3”]]
Error
III. II-III
mylist[“v1”]
[1] 1 2
IV.
mylist[1][1] I-II
[1] 1

Which of the followings are FALSE for lists? cbind can be used to add a new column.

Technically, a list is a vector.

c() notation is used to concatenate lists.

The columns of the lists can be accessed by using


$ sign.
Consider the data frame and the code below. Which graph is obtained by the code?
Name Height Weight
Ece 176 67
Irem 164 59
Deniz 174 64
Berk 179 73
Alperen 185 81
Melike 181 78
Bora 168 75
Onur 189 84
par(mfcol=c(1,2))
pie(students$Height,col=rainbow(8))
barplot(students$Weight,las=2)
par(mfrow = c(1,1))
pie(students$Height,col=rainbow(8))
barplot(students$Weight,las=2)
Consider we have following data frame. Which of the code scripts generates the following
figure?
Name Height Weight BMI above22.5 plot(person.data[,2:5],col=ifelse(person.dat
Can 1.70 65 22.49 FALSE a$above22.5,"red","green"))
Cem 1.75 66 21.55 FALSE
Hande 1.62 61 23.24 TRUE
Lale 1.76 64 20.66 FALSE
Arda 1.78 63 19.88 FALSE
plot(person.data[2:5],col=c("green","red"))

plot(person.data[,2:5],col=ifelse(person.data$BMI
>22.5,"green","red"))

plot(person.data[1,4],col=ifelse(person.data$abov
e22.5,"red","green"))
Which graph is obtained by the following code?
y <- c(1:100)
x <- c(1:100)
for(i in 1:100){
y[i] <- x[i]*x[i]-100*x[i]+2500}
plot(y,x)
The plot below is obtained by the code below. What should be A and B values based on your
linear regression knowledge? A=-1.5, B=0.5
y <- c(1:100)
x <- c(1:100)
x <- c(1:100)
for(i in 1:100){
y[i] <- 2*x[i]+3} A=3, B=2
model <- lm(x~y)
model

A=0, B=2

A=0, B=0.5

What should be the fifth row to obtain fallowing graph?


x <- sample(1:20,20)+rnorm(10,sd=2)
y <- x+rnorm(10,sd=3) abline(myModel,col="red")
myModel <- lm(y~x)
plot(x,y,xlim=c(1,30),ylim=c(1,35))
???????(5th row)????

lines(myModel,col="red")

plot(myModel,col="red")

lines(x,y,col="red")

We want to explain y values by using x and z values. Which of the code scripts generates myModel <- lm(y~x+z, data=df)
right model?
x <- sample(1:20,20)+rnorm(10,sd=2) myModel <- lm(y~x~z, data=df)
y <- x+rnorm(10,sd=3)
myModel <- lm(y,x,z, data=df)
z <- (sample(1:20,20)/2)+rnorm(20,sd=5)
df <- data.frame(z,y,x) myModel <- lm(y+x+z, data=df)

When the code below is executed we obtain the graph below. What is x equal to?
par(mfrow=c(1,3))
c(1,2,1,3)
pie(x)
barplot(x)
boxplot(x)

c(1,1,2,3)

c(3,2,1,1)

c(1,2,2,3,4,4,4)

What is sum equal to when the code is executed? 4


sum <- 0
alist <- list(vec1 = c(1,2), vec2 = c(3,4)) 10
for(i in alist){
6
sum <- i[1] + sum
} 3

What is the output of the following code?


7 9 11 11 13 13 13
k <- 1
stor <- 0
for(i in seq(3,14,2)){ 7 9 11 11 13 13 13 14 14 14
for(j in 2:5){
if((i/j)>3) {stor[k]=i
k <- k+1} 7 9 11 13
}
}
7 8 9 10 11 12 13 14
print(stor)
When the following code is executed it prints 8. What is the value of XYZ?
Hint: %% operator gives 1 if the number is odd, 0 if the number is 32516
even
spre <- function(myvec){
21113
sum <- 0
for(k in myvec){
ifelse(k%%2==1,sum <- sum+1 , sum <- sum*2)
12345
}
print(sum)
} 3 2 12 2 7
spre(XYZ)

Which statement is false? scan() can read any text file that contain
letters and numbers

scan() can be used to read input from keyboard.

read.table() and scan() can read data from web


URLs.

write.table() is a convenient way to save data to a


file.

Given the information below, Which one is the correct coding to get this output? scan("mytext.txt", what="", sep="\n")

Say we have the following text file, mytext.txt: scan("mytext.txt", sep="", what="\n")
1 2 3
a b 36 read.table("mytext.txt", header=FALSE,what="",
6.6 xyz sep="\n")

We want to get the following output: read.table("mytext.txt", header=FALSE,what="\n",


[1]"1 2 3" "a b 36" "6.6 xyz" sep="")

What is X, Y and Z? X=scan, Y=4, Z=readLines


We store the 1 midterm and 3 quiz grades of 100 students in
GRADES.txt. First we want to read the file in the matrix form and then X=read.table, Y=100, Z=readLines
obtain its elements line-by-line.
We write the following code:
X=read.table, Y=4, Z=readline
matrix(X("GRADES.txt"), ncol=Y,byrow=TRUE)
Z("GRADES.txt") X=scan, Y=100, Z=readline

What is the output of the following code?


afunc <- function(vec){ 53
if(length(vec)>5){
return(vec[1:5])
}
else{ 26
for(i in (length(vec)+1):5){
vec[i] <- i
} 45
}
return(vec)
}
mylist <- list(c(2,3,3),c(1,2,3),c(5,3,4)) 27
sum(sapply(mylist,afunc))

You have information about bank accounts of 20 people as vector "first_bank" and One of the vectors is recycled. So you should
"great_bank". When you sum them and assign the value to the vector "net", you received the check the lengths of your data.
warning message below, though the vector "net" has been created with 20 numerical
values.Which of the following is a reasonable explanation for the warning message? "net" is too short to be a vector name. Besides, it
is not descriptive enough. You should rename it
Warning message: according to the name conventions.
In first_bank + great_bank : You tried to execute a "multiple summation"
longer object length is not a multiple of shorter object length operation with two vectors which are not multiple
of each other. You should use ordinary summation
to get the values.

Probably it is just another case in which R exhibits


its oversensitivity about informing the user. You
should just keep working since R will take care of
tiny problems.

Which of the followings will be produced if order.vector is executed? 2,1,4,3,5


vector.data<-c(2,1,15,6,8)
vector.data<-sort(vector.data) 1,2,6,8,16
order.vector<-order(vector.data[c(2,1,4,3,5)])
4,2,1,3,5

1,2,6,15,8

Which of the following codes gives me only "Adam" column? hint: "&" indicate the logical i,iii
operations AND
Given: person.vector <- c(George=40,Judy=35,Jane=25,Adam=30) i,ii,iii
i) person.vector[person.vector>25 & person.vector<35]
i,ii
ii) person.vector[-c(-1,-2,-3)]
iii) person.vector[c(FALSE,FALSE,FALSE,TRUE)] ii,iii
Which of the following codes can create matrix below?
i,ii
[,1] [,2] [,3]
[1,] 1 7 13
[2,] 3 9 15 i,iii
[3,] 5 11 17

i)matrix(c(seq(1,11,2),13,15,17),nrow=3,byrow=F) ii,iv
ii)cbind(c(1,3,5),c(7,9,11),c(13,15,17))
iii)rbind(c(1,3,5),c(7,9,11),c(13,15,17))
iii,iv
iv)matrix(seq(1,17,2),nrow=3,byrow=T)

In order to update their new scores in the scores matrix, which of the following codes is
correct? scores[c(1,2,4),"GRE"] <- c(335,338,333)
scores matrix is defined as follows: scores["Tirole",3] <- 790

SAT GRE GMAT


Nash 2300 326 784
Shapiro 2345 328 795 scores[c(1,2,4),"GRE","GMAT"] <-
Stiglitz 2350 331 767 c(335,338,333,790)
Tirole 2245 320 774

4 students' SAT, GRE and GMAT scores are given in the matrix above. scores[c(Nash,Shapiro,Tirole),2] <-c(335,338,333)
Nash was not satisfied with his GRE score, and talked to Shapiro who scores["Tirole",3] <- 790
is also not satisfied with his GRE scores.
They decided to take these exams again.And also Tirole decided to take
both GRE and GMAT exam.
A couple of weeks later their new scores announced. Nash got 335, scores[c(1,2,4),c("GRE","GMAT")] <-
Shapiro raised his score to 338, Tirole got 333 from GRE and 790 from c(335,338,333,790)
GMAT

We would like to add a row to the matrix (given below) which contains sum of each fruit. rbind(mymatrix,"Total"=apply(mymatrix,2,su
Which of the following code can add a new row we want? m))
Apple Orange Mango
Einstein 10 2 12 cbind(mymatrix,"Total"=apply(mymatrix,2,sum))
Newton 20 4 45
rbind(mymatrix,"Total"=apply(mymatrix,1,add))
Descartes 30 6 21
rbind(mymatrix,"Total"=apply(mymatrix,1,add))

You might also like