R Programming Questions and Answers – Reading Datasets – 2

This set of R Programming Inteview Questions and Answers for freshers focuses on “Reading Datasets – 2”

1. What will be the Correct R code for the following output?

foo bar
1 1 TRUE
2 2 TRUE
3 3 FALSE
4 4 FALSE

a)

> x <- data.frame(foo = 1:4, bar = c(F, T, F, F))
> x

b)

advertisement
advertisement
> x <- data.frame(foo = 1:6, bar = c(F, T, F, F))
> x

c)

> x <- data.frame(foo = 1:4, bar = c(T, T, F, F))
> x

d)

advertisement
> x <- data.frame(foo = 14:1, bar = c(F, T, F, F))
> x
View Answer
Answer: c
Explanation: Data frames are used to store tabular data in R.
 
 

advertisement

2. Point out the wrong statement?
a) is.nan() is used to test objects if they are NA
b) is.nan() is used to test for NaN
c) NA values have a class
d) NA values have a class, so there are integer NA, character NA, etc
View Answer

Answer: a
Explanation: A NaN value is also NA but the converse is not true.

3. Data frames can be converted to a matrix by calling data _______
a) as.matr()
b) as.mat()
c) as.matrix()
d) as.max()
View Answer

Answer: c
Explanation: as.matrix() function should be used to coerce a data frame to a matrix.

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

> x <- data.frame(foo = 1:4, bar = c(T, T, F, F))
> ncol(x)

a) 2
b) 4
c) 7
d) 9
View Answer

Answer: a
Explanation: Data frames are represented as a special type of list where every element of the list has to have the same length.

5. Point out the correct statement?
a) Using factors with labels is better than using integers because factors are self-describing
b) Factors are used to represent categorical data and can be unordered or ordered
c) Factors are important in statistical modeling and are treated specially by modelling functions like lm() and glm()
d) All of the mentioned
View Answer

Answer: d
Explanation: Having a variable that has values “Male” and “Female” is better than a variable that has values 1 and 2.

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

 > x <- list("Los Angeles" = 1, Boston = 2, London = 3)

b)

 > names(x) <- c("New York", "Seattle", "Los Angeles")

c)

 > name(x) <- c("New York", "Seattle", "Los Angeles")

d)

 > names(x) <- c("New York", "Los Angeles", "Los Angeles")
View Answer
Answer: c
Explanation: Lists can also have names, which is often very useful.
 
 

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

> x <- 1:3
> names(x)

a) NULL
b) 1
c) 2
d) 4.5
View Answer

Answer: a
Explanation: R objects can have names, which is very useful for writing readable code and self-describing objects.

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

> m <- matrix(1:4, nrow = 2, ncol = 2)
> dimnames(m) <- list(c("a", "b"), c("c", "d"))
> m

a)

c d
a 1 3
b 2 4

b)

c d
a 1 2
b 2 3

c)

c d
a 1 3
b 4 2

d) Error
View Answer

Answer: a
Explanation: Matrices can have both column and row names.

9. Which of the following statement changes column name to h and f?
a) colnames(m) <- c(“h”, “f”)
b) columnnames(m) <- c(“h”, “f”)
c) rownames(m) <- c(“h”, “f”)
d) rownames(m) <- c(“f”, “f”)
View Answer

Answer: a
Explanation: Column names and row names can be set separately using the colnames() and rownames() functions.

10. Which of the following is used for reading tabular data?
a) read.csv
b) dget
c) readLines
d) writeline
View Answer

Answer: a
Explanation: read.table can also be used for reading dataset in structured form.

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 interviews, 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.