Midterm Session II #0000000224 - On March 25, 2016 14 13: Processing
Midterm Session II #0000000224 - On March 25, 2016 14 13: Processing
StudentID 2015300219
seq(5,by=1,length.out = 5)
seq(6,10,by=1)
Which one creates a different output?
6@10
seq(to=10,from=6)
-1 9 3 -3
-1 9 3 0
Which one is the output of c(-2,6,3) + c(1,3,0,-1)
-1 9 3 -1
-1 9 3 NA
rep(-2;2,c(2,1,2,1,2))
seq(-2@2,c(2,1,2,1,2))
Which one creates the sequence -2 -2 -1 0 0 1 2 2 ?
seq(2:-2,c(2,1,2,1,2))
rep(-2@2,c(-2,-1,0,1,2))
Which one creates vectors Real.GDP and Nominal.GDP by using the table Real.GDP <- c(492,27,280,224)
above? Nominal.GDP <- c(495,32,291,251)
Egypt 27 32
names(Real.GDP) <-
c("Japan","Egypt","France","Brazil")
names(Nominal.GDP) <-
c("Japan","Egypt","France","Brazil")
names(Real.GDP) <-
c(Japan,Egypt,France,Brazil)
names(Nominal.GDP) <-
c(Japan,Egypt,France,Brazil)
Which one gives names (Japan, Egypt, France, Brazil) to the vectors Real.GDP
and Nominal.GDP? name(Real.GDP) <-
c("Japan","Egypt","France","Brazil")
name(Nominal.GDP) <-
c("Japan","Egypt","France","Brazil")
names(Real.GDP) <-
("Japan","Egypt","France","Brazil")
names(Nominal.GDP) <-
("Japan","Egypt","France","Brazil")
Real.GDP[order(Nominal.GDP)]
Nominal.GDP[sort(Real.GDP)]
Which of the following sorts Real.GDP according to the Nominal.GDP?
Nominal.GDP(order[Real.GDP])
Real.GDP(sort[Nominal.GDP])
round(GDP.Deflator,2)
signif(GDP.Deflator,2)
Which of the following includes numbers which have exactly 2 decimal places
for the GDP.Deflator? trunc(GDP.Deflator,2)
floor(GDP.Deflator)
ifelse(GDP.Deflator<110,"Low
Inflation","High Inflation")
min(GDP.Deflator,2)
Which of the following creates GDP deflator groups "Low inflation" and "High
inflation"? ifelse(sort(GDP.Deflator,2),"Low
Inflation","High Inflation")
names(GDP.Deflator[GDP.Deflator<110])
names(GDP.Deflator[GDP.Deflator>110])
Which one gives the names of the countries whose GDP deflator is less than
110? names(sort(GDP.Deflator<110))
names(ifelse(GDP.Deflator<110,names))
i,ii
i.Lists can contain only one data type.
ii.Double brackets are one of the ways to reach elements of the list. i,ii,iii
iii.To concatenate lists, vector notation is used.
ii
>Student$Department<-NULL
>Student<-
c(Student[c(1,2)],Id=...,Student[c(4;5)])
Which of the following is the code that deletes the "Department" component
from list and adds "ID" as the third element of the list? >Del(Student$Department)
>Student[[3]]<-c(ID=...)
We have a list called “Student” whose elements are “Name”, “Age”,
>Student[["Department"]]<-NULL
“Department”and “GPA” in order. >Student$ID<-c(...)
>Del(Student$Department)
>Student<-
c(Student[c(1,2)],Id=...,Student[c(4@5)])
apply(Grades,var)
We have a list that contains the grades of students, named Grades. Each
element of the list corresponds to different courses. We want to learn the sapply(Grades,sd)
standard deviation of grades for every course in a list format.
lapply(Grades,var)
Given the matrix named Grades, which of the following code scripts does NOT Grades <- c(50,100,45) + c(90,100,70)+
generate Grades matrix? c(75,85,60)
Grades <-
Grades matrix is defined as follows: matrix(c(50,90,75,100,100,85,45,70,60),3,3)
50 100 45
90 100 70
matrix(c(50,90,75,100,100,85,45,70,60),3)
75 85 60
Given the matrix named Cars, which of the following code scripts returns the Cars["HONDA", "2nd Hand"]
2nd Hand price of HONDA ?
Cars[2,3]
Cars matrix is defined as follows: Cars [3[2]]
Cars [c(3,2)]
FIAT 32000 50000
HONDA 40000 25000
Given the matrix named Employees, what will be the output when 1234
"sort(order(Employee[,3]))" is executed ?
2413
Employee <-
cbind(Employee[,1;2],Employee[,3]*1.2)
Considering the same Employee matrix, let's say the headmaster decided to Employee <- Employee+Employee*20/100
increase all salaries by 20 percent. Which of the following code scripts update Employee <-
the Employee matrix correctly according to this new decision? cbind(Employee[c(1,2),],Employee[3,]*1.2)
Employee <-
apply(Employee,Employee[,3]*1.2)
Which of the following code scripts can be used in order to add this new Employee <- rbind(Employee,"Harun" =
person in the Employee matrix? c(20111912,22,3600))
Employee <-Employee +
Considering the same Employee matrix without any updates, assume the c("Harun",20111912,22,3600)
company hired a new person with the following informations :
Employee<-rbind("Harun" =
Name : Harun c(20111912,22,3600))
EmployeeID : 20111912
Age : 22 Employee<-Employee + rbind("Harun" =
Salary : 3600 c(20111912,22,3600))
Which of the following codes can NOT be used if you want to add a fourth person[4,] <- person[2,] / person[3,]^2
column of BMI?
person[[4]] <- person[[2]] / person[[3]]^2
Suppose you construct the following data frame: person$bmi <- person$weight /
person$height^2
person <- data.frame(name = c(“Guliz”,”Cansu”,”Sevil”), weight = c(80,65,68),
height = c(1.8,1.7,1.65))
person[,4] <- person[,2] / person[,3]^2
Which of the operations should be enough to realize your aim? Merge the two data frames by name and
team.
You have the following data frames:
Assign a set of ID's to each data frame, then
frame1: merge by ID.
Name Team City Bind the two data frames together by cbind.
1 Su FB Bursa
2 Berk FB Izmir
3 Cem GS Ankara
4 Tan FB Ankara
5 Su BJK Bursa
6 Naz GS Adana
frame2:
You assume that there cannot be two or more people of the same name and
team.
You want to have a new data frame with the people that are present only in
both of the data frames. You want to store all information related to a single
person in a row.
After making sure that the text file is located in your working directory, which read.table("abc.txt", header = T, sep = "-")
of the following codes should you run?
read(abc.txt, header=F, sep = "")
Your friend sends you a set of data as a text file (named abc.txt) for a project table("abc.txt", sep = ",")
you two are working on.
Model-Set-Solution
Linear-Convex-Yes
Nonlinear-Convex-No
Integer-Not Convex-No read.table(header = T, abc.txt, 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.
High-dimensional array
We should be careful about the arguments of the binding functions “rbind”
and “cbind” when we try to apply them to an X, because their arguments may Matrix
not always be of the same data structure.
None of the above.
Which one of the commands below should be run to access the population ankara[['population']]
component of the list? ankara$(population)
Which one of the commands below should have been executed in order to get ankara <- list("Ankara", 5270575, c(25,
such an output? 1430), FALSE, c("Cankaya","Besevler"))
Which one of the commands below should have been executed in order to get lapply(A, mean)
such an output?
sapply(A, mean)
The following command is executed: mean(A)
A <- list(c(7, 13, 17, 10, 3), c(9, 5, 8, 1, 6), c(14, 4, 20, 15, 16), c(2, 18, 11, 12,
19))
[[1]]
[1] 10
[[2]] lmean(A)
[1] 5.8
[[3]]
[1] 13.8
[[4]]
[1] 12.4
Which one of the commands below should have been executed in order to get sapply(A, mean)
such an output?
lapply(A, mean)
The following command is executed: mean(A)
A <- list(c(7, 13, 17, 10, 3), c(9, 5, 8, 1, 6), c(14, 4, 20, 15, 16), c(2, 18, 11, 12,
19))
Which one of the followings contains all the elements of the resulting data {a,b,c,g,h,i,j,k,1,2,3}
structure?
{a,b,c,d,e,f,g,h,i,j,k}
The following commands are executed: {j,k}
B <- list(c(“a”,”b”,”c”),c(1,2,3))
B <- list(B,c(“d”,”e”,”f”),c(“g”,”h”,”i”))
B[[2]]<-NULL
{a,b,c,j,k,1,2,3}
B[[3]]<-c(“j”,”k”)
Which of the following are not correct for data frames? iii
i,iii
i. Data frame is also a list.
ii. We can have numbers in one coloumn and character strings in another ii,iii
column of a data frame.
iii. Data frames do not have two dimensional rows and coloumns structure. i,ii,iii
In order to obtain "person_info" data frame which of the following can be i,ii
used?
i
person_info is defined as follows: ii
height weight initial
1 1.70 70 C
2 1.75 75 C
3 1.60 60 C
Which of the following CANNOT be used to obtain the departments of All of them can be used
students?
students$Department
students is defined as follows: students[,1]
ii
We have a text file in our directory which is “students_info”. This file contains
the following: i,iii
Which of the following binds these two data frames as below? merge(employees,employees_info,by="ID")
ID Height Weight
1 1.7 69
2 1.85 75 data.frame(employees,employees_info)
3 1.75 77
Which one of the commands below removes the vector (10,11,12) from the list? list3<-list3[[2]][-2]
list3<-list3[[3]][-2]
The following commands are executed:
list3<-list3[-4]
list1<-list(c(1,2,3),c(4,5,6))
list2<-list(c(7,8,9),c(10,11,12))
list3<-list(list1,list2) list3<-list3[[2]][[1]][-2]
1
The following commands are executed:
3
list1<-list(c(1,2,3),c(4,5,6))
list2<-list(c(7,8,9))
list3<-list(list1,list2) 4
Which command adds those 3 classes to our list? list3 <- mapply(rep,list3,2;3)
Suppose there is one more class with exactly same ages as of Class A and
there are two more classes with same ages as ClassB. Which command adds list3 <- mapply(rep,list3,seq(1,3,2))
those 3 classes to our list?
( mapply can be used to apply functions that require two inputs. )
Which command removes the Spouse vector from the list? person1<-person1[-3]
delete(person1$Spouse)
A list holds information about a person:
person1<- person1<-person1[[-3]]
list(Name=”Ebru”,Age=35,Spouse=”Mehmet”,Children=c(“Zeynep”,”Mehmet”))
Suppose Ebru divorces from Mehmet, which command removes the Spouse
person1[-[3]]
vector from the list?
sum(sapply(listr,sum))/6
Which command finds the grand average of all numbers?
sum(lapply(listr,sum))/6
A list is formed as follows:
listr<-list(c(3,5,7),c(2,4,6)) mean(lapply(listr,mean))
sapply(listr,mean)
34
What is the value of x?
3
y <- c(3,4,5)
x <- y[y<5] 345
4
Which of the following codes would help her to do her homework? apply(M,2,mean)
M
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
apply(M,1,var)
[1,] 1 4 6 10 13 16 19 72 25 28
[2,] 2 4 8 11 50 17 20 98 20
[3,] 3 5 9 12 15 6 21 24 27 30
Which of the following code would give her the countries that has the LOWEST sort(GDP/Population,decreasing=FALSE)
GDP per capita? [1]
sort(GDP/Population,decreasing=TRUE)[1]
A student has a homework which she needs to find country with lowest GDP
per capita. She has two vectors “Population” and “GDP” which includes 5 sort(GDP/Population,decreasing=FALSE)[5]
countries each. Which of the following code would give her the countries that
has the LOWEST GDP per capita? (GDP per capita can be found by dividing
GDP by Population.)
Which of the following would give the second and third rows of the matrix z? z[c(2,3),]
z[,c(2,3)]
Matrix “z” is defined as follows:
[,1] [,2] [,3] z[;2,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9 z[2,3]
15
Binary matrix is defined as follows:
[,1] [,2] [,3] [,4] 8
[1,] 1 1 1 1
[2,] 1 0 0 1
[3,] 1 0 0 1
[4,] 1 1 1 1
A student has a matrix called “Binary” as above,in which every row resembles 16
a number. The student writes a code in order to convert 3rd row to decimal,
which is as follows:
x <- Binary[3,][1]*2^3+Binary[3,][2]*2^2+Binary[3,][3]*2^1+Binary[3,][4]*2^0
Which of the below gives the correct function creation and monthly cost cost.function <-function(x,y,z,t) { x*
matrix for the firms? (1000*y + z) + t }
cost.matrix <- cost.function(trucks,
M1 M2 M3 M4 M5 M6 costs[,1],costs[,3],costs[,2])
M7 M8 M9 M10 M11 M12
A 56 59 66 65 61 74 cost.function<-function(x,y,z,t) { x*(1000*y +
82 82 68 55 52 59 z) + t }
cost.matrix <- function(trucks,Fuel Cost per
B 36 47 45 51 57 57
KM,Maintenance Cost per Thousand KM,Fixed
61 57 53 55 50 48 Cost)
C 70 64 66 72 58 55
42 44 60 78 71 54 cost.function<- cost.function(x,y,z,t) { x*
D 36 34 37 32 28 40 (1000*y + z) + t }
44 45 39 33 28 24 cost.matrix <- cost.function(trucks,
Monthly Truck Usage for 4 Different costs[1,],costs[3,],costs[2,])
Construction Companies
In order to update their new scores in the scores matrix, which of the following scores[c(1,3,4),"TOEFL"] <- c(110,114,112)
codes is correct? scores[“Aziz","Proficiency"] <- “A”
scores[c(“Nuri","Orhan","Aziz"),1] <-
TOEFL IELTS Proficiency c(110,114,112)
Nuri 109 8 B scores[“Aziz",3] <- A
Daron 118 8.5 A
Orhan 103 8.5 A
Aziz 105 8 C
4 studentsʼ scores are given in the matrix above. Nuri and Orhan were not
satisfied with their TOEFL scores and decided to take these exams again.
During the examinations, they ran into Aziz who was taking TOEFL and scores[c(1,3,4),c("TOEFL","Proficiency")] <-
c(110,114,112,"A")
Proficiency on the same day. A couple of weeks later their new scores
announced.
Nuri and Orhan got 110 and 114 from TOEFL respectively, Aziz got 112 from
TOEFL and A from Proficiency.
i, ii
lost matrix is defined as follows:
i, iii
D E F
A 1 1 2
B 4 8 15
C 16 23 42
i. lost > lost[,1] statement gives a matrix whose first column elements are
i, ii, iii
FALSE, others are TRUE
ii. lost[lost <= lost[2,3]] statement yields a vector of (1,4,1,8,2,15)
iii. lost[1,] <- lost[lost<=lost[1,3]] statement does not make any changes to
lost matrix.