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)

```> 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```
Explanation: 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

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()

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

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

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")`
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

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

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”)

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?
b) dget
d) writeline

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]