R Programming Inteview Questions and Answers for freshers focuses on “Reading Datasets”
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)
> 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)
> x <- data.frame(foo = 14:1, bar = c(F, T, F, F)) > x
View Answer
Answer: c
Clarification: Data frames are used to store tabular data in R.
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
Answer: a
Clarification: 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()
Answer: c
Clarification: 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
Answer: a
Clarification: 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
Answer: d
Clarification: 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
Clarification: Lists can also have names, which is often very useful.
7. What will be the output of the following R code?
a) NULL
b) 1
c) 2
d) 4.5
Answer: a
Clarification: R objects can have names, which is very useful for writing readable code and self-describing objects.
8. 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”)
Answer: a
Clarification: Column names and row names can be set separately using the colnames() and rownames() functions.
9. Which of the following is used for reading tabular data?
a) read.csv
b) dget
c) readLines
d) writeline
Answer: a
Clarification: read.table can also be used for reading dataset in structured form.