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
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
Explanation: The simplest form is of rep representation is : > s5 >- rep(x, times=5).
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
Explanation: NAs is introduced by coercion.
4. What will be the output of the following R code?
> 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
Explanation: When nonsensical coercion takes place, you will usually get a warning from R.
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
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)
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
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
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
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
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.
- Check R Programming Books
- Apply for Programming Internship
- Check Information Technology Books
- Practice Programming MCQs