R Programming Questions and Answers – Data Types – 3

This set of R Programming Questions and Answers for freshers focuses on “Data Types – 3”.

1. Which of the following statement would print “0” “1” “2” “3” “4” “5” “6” for the following R code?

 > x <- 0:6

a) as.character(x)
b) as.logical(x)
c) as.numeric(x)
d) as.num(y)
View Answer

Answer: a
Explanation: as.character would print number from 0 to 6.

2. Point out the correct statement?
a) rep() is be used for replicating an object in various complicated ways
b) seq() function has four arguments
c) sequence() is a more general facility for generating sequences
d) numerical vectors are generated by conditions
View Answer

Answer: a
Explanation: The simplest form is of rep representation is : > s5 >- rep(x, times=5).

advertisement
advertisement

3. What will be the output of the following R code?

x <- c("a", "b", "c")
> as.numeric(x)

a) Warning
b) Error
c) Abnormal termination
d) Prints x
View Answer

Answer: a
Explanation: NAs is introduced by coercion.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

4. What will be the output of the following R code?

advertisement
> x <- c("a", "b", "c")
> as.logical(x)

a) a b c
b) NA NA NA
c) 0 1 2
d) 6 8 9
View Answer

Answer: b
Explanation: When nonsensical coercion takes place, you will usually get a warning from R.

advertisement

5. Point out the correct statement?
a) The elements of a logical vector cannot have the values TRUE, FALSE, and NA
b) Matrices are vectors with a dimension attribute
c) Numerical vectors are generated by conditions
d) seq() function has four arguments
View Answer

Answer: b
Explanation: The dimension attribute is itself an integer vector of length 2 (number of rows, number of columns).

6. Which of the following is a valid assignment?
a)

 > m <- matrix(nrow = 2, ncol = 3)

b)

 > m <- matrix(nrow = 2, ncol = 3.5)

c)

 > m <- mat(nrow = 2, ncol = 3)

d)

 > m <- mat(nrow = 2, ncol = 5)
View Answer
Answer: a
Explanation: The valid assignment is: m lt;- matrix(nrow = 2, ncol = 3). It creates a matrix ‘m’ with 2 rows and 3 columns. The other assignments are invalid because they either have a decimal value for the number of columns (which should be a positive integer) or use an incorrect function name ‘mat’ instead of ‘matrix’.
 
 

7. What will be the output of the following R code?

 > m <- matrix(nrow = 2, ncol = 3)
 > dim(m)

a) 3 2
b) 2 3
c) 2 2
d) 4 5
View Answer

Answer: b
Explanation: Matrices are constructed column-wise.

8. What will be the output of the following R code?

> m <- matrix(1:6, nrow = 2, ncol = 3)
> m

a)

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

b)

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

c)

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

d)

[,5] [,6] [,7]
[1,] 1 3 6
[2,] 2 4 5
View Answer
Answer: a
Explanation: Matrices can also be created directly from vectors by adding a dimension attribute.
 
 

9. What will be the output of the following R code?

> m <- 1:10
> m

a) 1 2 3 4 5 6 7 8 9 10
b) 1 2 3 4 5 6 7 8 9 10 11
c) 0 1 2 3 4 5 6 7 8 9 10
d) 10 9 8 6 5 4 2 3 1 2
View Answer

Answer: a
Explanation: Matrices can be created by column-binding.

10. What will be the output of the following R code?

> x <- 1:3
> y <- 10:12
> cbind(x, y)

a)

x y
[1,] 6 10
[2,] 7 11
[3,] 8 12

b)

x y
[1,] 1 10
[2,] 2 11
[3,] 3 12

c)

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

d)

x y
[1,] 1 6
[2,] 2 5
[3,] 3 10
View Answer
Answer: b
Explanation: Roughly cbind() forms matrices by binding together matrices horizontally, or column-wise, and rbind() vertically, or row-wise.
 
 

Sanfoundry Global Education & Learning Series – R Programming Language.

Here’s the list of Best Books in R Programming Language.

To practice all areas of R Programming for Freshers, Here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.