Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Midterm (MT) Preparation

Set A - Answer the following questions:

a. Which of the following is a logical operator?

> %in% * ^

ANS:

b. What value does R return in the statement below?

> -3 <= - 7

ANS:

c. What is the result of this calculation?

> (-45 + 5) / 10 + (-1)^(-2)

ANS:

d. You entered and executed in your R session this code for numerical variables x and y: >  x <- - 150

> y<- 10^2

How would R evaluate each of the following?

> x + y

ANS:

e. How would R evaluate the following?

> 2 = (6-4)

ANS:

f. What is the result of this code?

> x<- c(-1, abs(-2))

> x^5

ANS:

g. What is the result of this code?

> sqrt(16) == 15

ANS:

h. What is the result of this code?

> u<- “university”

> nchar(u)

ANS:

i. What is the result of this code?

> a<- “Hello

> class(a)

ANS:

Set B - Answer the following multiple choice questions:

1) You want to view the following data frame in R console:

> d <- data.frame (c(1,2,3), c(t, m, n))

What is a correct R code to view that data frame in R?

A) >print d’

B) > print (d)

C) > print d

D) > view(d)

E) > view (“d”)

F) None of the above can do that ANS:

2) Type the code in R that you will use to produce the following comment:

“I understand that today we are doing a MT review” .

ANS:

3) You had previously used the following code to create the variable v in your R session:

> v<- c(1, 2, 3, 4)

Remove the variable v from your R session.

ANS:

4) Show the result of this code:

>r<- c(4, 6, 8)

m<-c(“school”, “boy”, “girl”)

>data <-data.frame (r, m)

>str(data)

ANS:

4) Show the result of this code:

>r<- c(4, 6, 8)

m<-c(“school”, “boy”, “girl”)

>data <-data.frame (r, m)

>class(r)

ANS:

5) Show the result of this code:

>r<- c(4, 6, 8)

m<-c(“school”, “boy”, “girl”)

>data <-data.frame (r, m)

>class(m)

ANS:

6) What does the following code produces?

Be aware of the result: would that be integer, numeric, logic, character, etc...

>v<- - 1:2

> print(v)

A) it produces the following result: [1]  - 1 0 1 2

B) it produces the following result: [1] – 1/2

C) it produces the following result: [1] – 0.5

D) it produces the following result: [1] – 1  2

E) it produces none of the above mentioned results ANS:

7) In your current R session console you execute the following code:

>p<- seq(-1, 2, by =0.5). When you execute it, it produces which of the following:

A) it produces the variable seq: [1] 2

B) it produces the variable p: [1] – 1

C) it produces the sequence seq: [1] - 1  -0.5  0  0.5  1   1.5   2

D) it produces the sequence p: [1] - 1  -0.5  0  0.5  1   1.5   2

E) it produces the sequence seq: [1] – 1  1

F) it produces the variable p:

[1] – 1  1

ANS:

8) You create the following variable:

> k <- c(0.5, 5.5 and 7)

The variable k is of what data type ...?

A) long type

B) short data type

C) integer

D) numeric

E) logical

F) None of the above

9) True or False: The working directory is the default location where R will look for files you want to load and where it will put any files you save.

ANS…

10) What does the following code tells you?

 

ANS:

11) Following is a code that I claim is going to give me a vector of character type. >plant<- c(“blackberry”, “apple, “peach”)

Is anything wrong with the code? Comment.

12) Following is a code that I claim is going to give me a vector of numeric type. >size<- c(14, 12, “5”)

Is anything wrong with the code? Comment.

13) Following is a code that I claim is going to give me a vector of integer type. >size<- c(14, 12, 5)

Is anything wrong with the code? Comment

14) Which one can be used to create matrix in R?

A) matrix()

B) m()

C) data.frame()

D) dataframe()

E) None of the above ANS:

15) The following code was used to create the data frame K

 

What is the first column of the created data set and what are its values? ANS:

16) The following code was used to create the data frame K

 

What are the variables that are created by the R program? Name them. ANS:

17) You create a data frame in your R session, but you did not name the columns in your data frame. How is the R program going to distinguish between columns in the data frame? So how is it going to refer to column 1, column 2, ...?

ANS:

Part 3:

The following code was used to construct the sample data frame and to view it in your R session.

> df <- data.frame(student = c("John","Alex","Smith","Greg","Martin"), Grade = c("A","A","B","C","C"), marks = c(89,82,75,63,58))

> print(df)

The data frame looked like this:

 

Write the code that will produce the following results:

a) Write the code that will extract and displays the data value 63 from this data frame. Name the extracted information m.

ANS:

b) Write the code that extracts and displays the min of column mark from this data frame. Name the extracted information p:

ANS:

c) Write the code that extracts and displays only the columns student and grade from this data frame. Name the extracted information f.

ANS:

d) Write the code that extracts and displays only the observations (rows) that have a value > = 75 in the marks column. Name the extracted information q.

ANS:

>q<-df (df$marks >=75, ]

>q