Welcome To Cmpe140 Final Exam: Studentid
Welcome To Cmpe140 Final Exam: Studentid
Management
CMPE140 – Spring 2016
Exam
StudentID *
Write here
What is X, Y and Z? *
We store the 1 midterm and 3 quiz grades of 100 students in GRADES.txt. First we want to read
the 㐰㈠le in the matrix form and then obtain its elements line-by-line.
matrix(X("GRADES.txt"), ncol=Y,byrow=TRUE)
Z("GRADES.txt")
10000 random numbers following normal distribution has been created. In order to plot the
histogram, we de㐰㈠ne an x-axis range of (-10,10) and the data intervals are supposed to be of
length 0.10.
x<-rnorm(10000)
hist(x,range=c(-10,10), by=0.10))
x<-rnorm(10000)
hist(x,breaks=(-10,10, 0.10))
x<-rnorm(10000)
x<-rnorm(10000)
(hint: %% is the modulo operator in R. This operation 㐰㈠nds the remainder after division of one
number by another. For example, 5%%2 is 1. )
Consider the following function:
modula.generator <‐ function(vec){
container.vec <‐ vector()
for(i in 1:length(vec)){
if(vec[i] %% i == 2){
container.vec <‐ c(container.vec,vec[i])
}
}
return(container.vec)
}
24
11
2 11 16
func <‐function(n){
i<‐1
sum<‐0
while(i<n){
sum<‐sum +i
i<‐i+1
}
repeat{
if(sum%%2==0){
break
}
sum<‐sum‐1
}
return(sum)
}
func(6)
(hint: %% is the modulo operator in R. This operation 㐰㈠nds the remainder after division of one
number by another. For example, 5%%2 is 1. )
15
14
10
20
Which of the matrix operations, in terms of an R code, does the above code perform? *
[,1] [,2] [,3]
[1,] 1 3 2
[2,] 4 6 5
for(i in 1:dim(A)[2]){
for(j in 1:dim(A)[1]){
B[i,j]<‐A[j,i]
}
}
By carefully inspecting the information given, which of the matrix operations, in terms of an R
code, does the above code perform?
B <- inverse(A)
B <- apply(A)
B <- t(A)
B <- diag(A)
Risk premium on lending is the interest rate charged by banks on loans to private sector
customers minus the “risk free” treasury bill interest rate
at which short-term government securities are issued or traded in the market. In some countries
this spread may be negative,
indicating that the market considers its best corporate clients to be lower risk than the
government.
An analyst 㐰㈠nds the corresponding data below for 5 di㈷㜶erent countries (Canada, USA, Brazil, UK
and Switzerland) for the time period of 2011 to 2014.
data <‐ c(2.1, 2.0, 2.0, 2.1, 3.2, 3.2, 3.2, 3.2, 32.2, 28.6, 18.4, 20.5, ‐0.01, 0.2, 0.2,
But the problem is the data is given in a vector form. The analyst wants to convert the data into a
matrix called RPonL with the years on the columns
and the countries placed in the rows. After that, she is asked to 㐰㈠nd the mean of the risk
premiums for each countries and re㌮㌷ect the results
in a column in the existing matrix.
Name Height Weight BMI
1 Mahmut 1.72 68 22.99
2 Ece 1.76 67 21.63
3 Sevde 1.64 59 21.94
4 Deniz 1.74 64 21.14
5 Berk 1.79 73 22.78
6 Alperen 1.85 81 23.67
7 Melike 1.81 78 23.81
8 Bora 1.68 75 26.57
9 Irem 1.89 84 23.52
10 Onur 1.65 70 25.71
ifelse(student.frame$BMI<21.5,"Slim","Normal")
Deniz and Ece
Deniz
Mahmut, Ece, Sevde, Berk, Alperen, Melike, Bora, Irem and Onur
Bora
i. It is impossible to overwrite a changed data set into an existing 㐰㈠le with write.table.
ii. We can download graphics both in png and pdf format.
iii. We can specify the seperator for the write.table command.
i, iii
iii
ii, iii
You have information about bank accounts of 10 people (for research purposes). Each has one
account at First Bank and one account at Great Bank. The vectors “㐰㈠rst_bank” and “great_bank”
contain the amounts at First Bank and Great Bank, respectively. When you sum them and assign
the value to the vector “net”, you received the following warning message, though the vector
“net” has been created with 10 numerical values:
Warning message:
In first_bank + great_bank :
longer object length is not a multiple of shorter object length
You tried to execute a "multiple summation" operation with two vectors which are not
multiple of each other. You should use ordinary summation to get the values.
One of the vectors is recycled. So you should check the lengths of your data.
"net" is too short to be a vector name. Besides, it is not descriptive enough. You should
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.
123
a b 36
6.6 xyz
[1]"123" "a b 36" "6.6 xyz"
scan("mytext.txt", what="\n")
scan("mytext.txt", sep="\n")
partial.sums <‐ function(vec){
sum.numbers <‐ 0
sums.seq <‐ vector()
for(i in 1:length(vec)){
sum.numbers <‐ sum.numbers + vec[i]
sums.seq <‐ c(sums.seq,sum.numbers)
}
return(sums.seq)
}
550
50
5 0 -5
55
Name Height Weight
Ece 1.76 67
Irem 1.64 59
Deniz 1.74 64
Berk 1.79 73
Alperen 1.85 81
Melike 1.81 78
Bora 1.68 75
Onur 1.89 84
par(mfcol=c(1,2))
pie(sales,col=rainbow(6))
barplot(sales,las=2)
par(mfrow = c(1,1))
What is the correct output if the following code script is executed? *
myfunction<‐function(vector){
i<‐1
count <‐0
while(i<=length(vector)){
if(i > vector[i]){
vector[i]<‐0
}
i<‐i+1
}
vector<‐vector[vector>0]
return(vector)
}
myfunction(c(6,-1,3,5,-4,12,4,10))
-1 -4
10 4 12 -3 5 3 -1 6
6 3 5 12 4 10
6 3 5 12 10
Name Height Weight BMI above22.5
Can 1.70 65 22.49 FALSE
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)
plot(person.data[1,4],col=ifelse(person.data$above22.5,"red","green"))
plot(person.data[2:5],col=c("green","red"))
plot(person.data[,2:5],col=ifelse(person.data$above22.5,"red","green"))
ages <‐ c(7,8,8,9,10,12,13,14,15,15,16,16,17)
highest 25%.
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 o㈷㜶 the lowest 25% of the data from
my.vector <‐ c(37,15,21,4)
my.vector <‐ sort(my.vector)
ordered.vector <‐ order(my.vector[c(4,2,1,3)])
4 15 21 37
3241
1234
4213
Which of the following codes should be run to view the eye color distribution of black-haired male
students and the hair color distribution of blue-eyed female students side by side? *
“HairEyeColor” is one of the built-in data sets in R. In the array the hair and eye color of the
students in a statistics class is stored. Below is the content of the array:
, , Sex = Male
Eye
Hair Brown Blue Hazel Green
Black 32 11 10 3
Brown 53 50 25 15
Red 10 10 7 7
Blond 3 30 5 8
, , Sex = Female
Eye
Hair Brown Blue Hazel Green
Black 36 9 5 2
Brown 66 34 29 14
Red 16 7 7 7
Blond 4 64 5 8
par(mfrow=c(1,1))
pie(HairEyeColor[1,1])
pie(HairEyeColor[2,2])
par(mfrow=c(1,2))
pie(HairEyeColor[1,,1])
pie(HairEyeColor[,2,2])
par(mfrow=c(1,2))
pie(HairEyeColor[,1,1])
pie(HairEyeColor[,2,2])
par(mfrow=c(1,1))
pie(HairEyeColor[1,1,],HairEyeColor[2,,2])
The following code script will be used to 㐰㈠nd the 㐰㈠bonacci numbers until a positive number n.
fibos<‐function(n){
i<‐3
fibonacci.numbers<‐c(1,1)
fibo.number<‐2
while(X<=n){
fibonacci.numbers[i]<‐fibo.number
fibo.number <‐ fibo.number+Y
i<‐i+1
}
return(fibonacci.numbers)
}
㐰㈠bos(20)
X = 㐰㈠bo.number , Y = 㐰㈠bonacci.numbers[i-1]
X = i, Y = 㐰㈠bonacci.numbers[i]
X = 㐰㈠bo.number , Y = i
X = i , Y = 㐰㈠bonacci.numbers[i-1] + 㐰㈠bonacci.numbers[i-2]
The output of which of the following codes is the most suitable if we want to view and compare
the boxplots of the given data? *
A class of 8 students is divided into two sections. We have the grades of their 㐰㈠rst maths
midterm and their birth years stored in the vectors below:
grades_section_1 <‐ c(90,80,73,20)
grades_section_2 <‐ c(88,36,65,29)
birth_year<‐ c(1994,1994,1995,1993,1994,1994,1995,1994)
par(mfrow=c(1,2))
boxplot(grades_section_1,grades_section_2)
boxplot(birth_year)
par(mfrow=c(1,1))
boxplot(grades_section_1,grades_section_2,birth_year)
par(mfrow=c(1,1))
boxplot(grades_section_1,grades_section_2,birth_year,ylim=c(20,1993))
par(mfrow=c(1,3))
boxplot(grades_section_1)
boxplot(grades_section_2)
boxplot(birth_year)
A <‐ matrix(scan("aa.txt"), ncol=3, byrow=TRUE)
B <‐ matrix(scan("bb.txt"), ncol=3, byrow=TRUE)
C <‐ A%*%B
123
456
789
123456789
001
010
100
987
654
321
Suppose John is replaced by Ramsay, which one of the below replaces John with Ramsay? *
s1 <‐ list(Name="Arya",Age=16,Class=10,Gender="F",Teachers=c("John","Emilia"))
stud.grade
Name MT1 MT2
1 Cem 65 58
2 Ali 80 85
3 Burcu 55 75
stud.info
Name Dept Year
1 Ali EC 2
2 Burcu IE 4
3 Cem EE 3
We want to merge these two data frames into one which has both the info about and grades of
the same student.
I, II, III
I, II
II, III
newlist <‐ list(c(1,2,3),c(4,5,6))
mean(lapply(newlist,mean))
mean(sapply(lapply(newlist,mean),sum))
lapply(newlist,mean)
sapply(newlist,mean)
Which one of the commands below can be used to get number 11? *
a <‐ list(c(1,2,3),c(4,5,6))
b <‐ list(c(7,8,9),c(10,11,12))
d <‐ list(a,b)
d[[4]][2]
d[[2]][[2]][2]
d[11]
d[[2]][5]
Which one is the most convenient way to read in a rectangular grid of data from a text file? *
readline()
read.table()
scan()
readLines()
In linear algebra, a circulant matrix is a special kind of Toeplitz matrix where each row vector is
rotated one element to the right relative to its predecessor row vector (i.e. a row is constructed
by shifting the upper row to the right by 1 element). In numerical analysis, circulant matrices are
important because they are diagonalized by a discrete Fourier transform, hence linear equations
that contain them may be quickly solved using a fast Fourier transform. An example of such a
matrix is given below:
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 4 1 2 3
[3,] 3 4 1 2
[4,] 2 3 4 1
In this question, the aim is to create the nxn circulant matrix called CRC by using the X vector.
The 㐰㈠rst row of the CRC is the X vector and the other rows are created by shifting the 㐰㈠rst row.
for(i in 1:length(X)){
‐ ‐ ‐ ‐ ‐ ‐ ‐ ‐
}
Ali has a data frame “grade” which keeps names, midterm and 㐰㈠nal results and number of
absent days of 50 students respectively.
With that data Ali 㐰㈠rst adds a column for absence point which is calculated as: students are
ordered from the least absent to the most.
Then the 㐰㈠rst student gets 100, the second 98, the third 96, etc.
Then he wants to calculate the total grade of each student with
total=mt*0.4+final*0.5+abs*0.1
After he gives the total points, he wants to delete the last 5 students from the list as they failed
the course. In order to do these jobs, Ali writes the below code:
(I) grade<-grade[order(grade[[4]]),]
(II) grade[[5]]<-seq(100,0,-2)
(III) grade[6,]<-grade[2,]*0.4+grade[3,]*0.5+grade[5,]*0.1
(IV) grade<-grade[order(grade[[6]],decreasing=T),]
(V) grade<-grade[-1:-5,]
II, IV
I, III
III, V
I, IV
Which of the following does the increment (by 10) and also ensures that nobody will have a final
score higher than 100? *
The instructor of cmpe 140 course is impressed by the performance of students such that he/she
wants to add extra 10 points to the 㐰㈠nal scores of everyone whose 㐰㈠nal score is higher than
his/her midterm score. Final and midterm scores are stored in the vectors named “㐰㈠nals” and
“midterms”.
Aslı has data which contain the students’ names and grades.
Aslı has to 㐰㈠nd students who are eligible to take the 㐰㈠nal exam and store them as a Boolean
entry in Eligible column . In order to take the 㐰㈠nal exam one has to have an average above 65. In
order to 㐰㈠nd these students Aslı follows these steps :
I. Aslı creates a matrix “Grade” which stores the data and has columns “Name”,”MT1″ and “MT2”
respectively.
II. She adds a column “Avg” which holds the average grades of students by using Grade$Avg<-
(Grade[[2]]+Grade[[3]])/2
III. Then she adds a column “Eligible” by using Grade$Eligible<-ifelse(Grade[[4]]>65,T,F)
II
I, II, III
Submit