R Data Frames MCQ

1. What is a data frame in R?

a) A special type of list
b) A matrix with named rows and columns
c) A table-like structure with rows and columns of equal length
d) A collection of vectors of different types

Answer:

c) A table-like structure with rows and columns of equal length

Explanation:

A data frame in R is a table-like structure where columns represent variables and rows represent observations. Each column can contain different types of data, but all columns must have the same number of rows.

2. How do you create a data frame in R?

a) dataframe()
b) df()
c) data.frame()
d) create.df()

Answer:

c) data.frame()

Explanation:

The data.frame() function is used to create a data frame in R.

3. How can you access the third column of a data frame named 'df' in R?

a) df[3]
b) df[[3]]
c) df$third
d) Both a and b

Answer:

d) Both a and b

Explanation:

You can access the third column of a data frame using either df[3] or df[[3]]. If the column has a name, you can also use df$columnName.

4. How do you add a new column to an existing data frame in R?

a) append()
b) df$newColumn <- values
c) addColumn()
d) insert()

Answer:

b) df$newColumn <- values

Explanation:

To add a new column to a data frame, you can use the syntax df$newColumn <- values, where 'newColumn' is the name of the new column.

5. Which function is used to view the structure of a data frame in R?

a) view()
b) structure()
c) str()
d) describe()

Answer:

c) str()

Explanation:

The str() function is used to display the structure of an object in R, including a data frame.

6. How do you select rows from a data frame based on a condition in R?

a) df[condition, ]
b) select(df, condition)
c) df[which(condition), ]
d) Both a and c

Answer:

d) Both a and c

Explanation:

You can select rows from a data frame based on a condition using df[condition, ] or df[which(condition), ].

7. What does the merge() function do with data frames in R?

a) Merges the rows of two data frames
b) Merges the columns of two data frames
c) Combines two data frames based on a common column
d) Stacks two data frames vertically

Answer:

c) Combines two data frames based on a common column

Explanation:

The merge() function is used to combine two data frames by matching rows based on one or more common columns.

8. How do you remove rows with missing values (NA) from a data frame in R?

a) na.omit(df)
b) remove.na(df)
c) df[!is.na(df)]
d) clean(df)

Answer:

a) na.omit(df)

Explanation:

The na.omit() function removes rows with missing values (NA) from a data frame in R.

9. Which function can be used to sort a data frame by a specific column in R?

a) sort()
b) order()
c) arrange()
d) orderby()

Answer:

b) order()

Explanation:

The order() function is used to sort a data frame by a specific column. You can use it with the data frame indexing to rearrange the rows.

10. How do you convert a matrix to a data frame in R?

a) as.dataframe()
b) as.df()
c) data.frame()
d) as.data.frame()

Answer:

d) as.data.frame()

Explanation:

The as.data.frame() function is used to convert a matrix or other object types to a data frame in R.

11. What is the function head() used for in R?

a) Returns the first few rows of a data frame
b) Returns the header of a data frame
c) Creates a new data frame
d) Sorts the data frame in ascending order

Answer:

a) Returns the first few rows of a data frame

Explanation:

The head() function is used to return the first n rows of a data frame (n=6 by default).

12. How do you rename columns in a data frame in R?

a) rename(df, oldName = newName)
b) df$oldName <- newName
c) names(df)[names(df) == "oldName"] <- "newName"
d) df$oldName <- df$newName

Answer:

c) names(df)[names(df) == "oldName"] <- "newName"

Explanation:

To rename a column in a data frame, you can use the names() function to replace the old column name with a new one.

13. What does the summary() function do for a data frame in R?

a) Provides a detailed description of each column
b) Summarizes the data with statistics like mean, median
c) Gives the total number of rows and columns
d) Displays the first and last few rows

Answer:

b) Summarizes the data with statistics like mean, median

Explanation:

The summary() function provides a summary of each column in a data frame, including statistics like mean, median, min, max, and quartiles for numeric columns.

14. How do you subset a data frame for specific columns in R?

a) df[c("col1", "col2")]
b) subset(df, select = c("col1", "col2"))
c) df$col1, df$col2
d) Both a and b

Answer:

d) Both a and b

Explanation:

You can subset a data frame for specific columns either by using df[c("col1", "col2")] or the subset() function.

15. How do you convert a data frame to a matrix in R?

a) as.matrix(df)
b) matrix(df)
c) convert.matrix(df)
d) to.matrix(df)

Answer:

a) as.matrix(df)

Explanation:

The as.matrix() function is used to convert a data frame to a matrix in R.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top