MCQs on Basic Data Manipulation | R

Learn the fundamentals of data manipulation in R, including efficient techniques for indexing, subsetting, filtering, sorting, combining, and reshaping data to handle large datasets in your analysis.


MCQs on Basic Data Manipulation in R

1. Indexing and Subsetting Data

  1. What function is used to index specific elements in a vector in R?
    a) vector[]
    b) vector(index)
    c) index(vector)
    d) vector[[]]
  2. How do you extract the second element from a vector v in R?
    a) v[2]
    b) v[1]
    c) v(2)
    d) v[[2]]
  3. What is the output of df[1:3,] in R?
    a) The first three rows of df
    b) The first three columns of df
    c) The entire first row of df
    d) A random subset of df
  4. In R, how can you subset a data frame df to select rows where Age > 30?
    a) df[df$Age > 30, ]
    b) df[Age > 30, ]
    c) df[Age > 30]
    d) df[Age = 30, ]
  5. How do you select the third column of a data frame df in R?
    a) df[, 3]
    b) df[3, ]
    c) df[3]
    d) df$3
  6. What does the drop=FALSE parameter do when subsetting data in R?
    a) It prevents R from dropping dimensions when selecting a single row or column
    b) It drops the dimensions of the data frame
    c) It causes R to drop all data that doesn’t match the condition
    d) It drops rows that contain NA values
  7. How do you subset a data frame to include rows where the value in column X equals 5?
    a) df[df$X == 5, ]
    b) df[X == 5, ]
    c) df[X == 5]
    d) df[X = 5, ]
  8. What does the function subset() do in R?
    a) It subsets a data frame based on a condition
    b) It combines multiple data frames
    c) It sorts the data in a frame
    d) It removes NA values from a data frame
  9. How can you extract multiple columns from a data frame df in R?
    a) df[, c(1, 2)]
    b) df[c(1, 2)]
    c) df[1:2]
    d) df[, 1:2]
  10. What does df[ , -1] do to a data frame df?
    a) Removes the first column
    b) Removes the first row
    c) Selects the first column
    d) Selects the first row

2. Filtering and Sorting

  1. Which function is used to filter rows based on a condition in R?
    a) filter()
    b) select()
    c) subset()
    d) sort()
  2. How would you sort a data frame df by the column Age in ascending order?
    a) df[order(df$Age), ]
    b) df$Age[sort()]
    c) sort(df$Age)
    d) df$Age[order()]
  3. Which function is used to sort a vector in R?
    a) order()
    b) sort()
    c) rank()
    d) filter()
  4. How do you sort a data frame df by multiple columns, Age and Salary, in R?
    a) df[order(df$Age, df$Salary), ]
    b) df[sort(df$Age, df$Salary), ]
    c) df[order(df$Age + df$Salary), ]
    d) df[sort(df$Age, df$Salary)]
  5. What does the arrange() function do in R?
    a) Sorts data in ascending or descending order
    b) Filters rows based on conditions
    c) Joins two data frames
    d) Summarizes data by a group
  6. How do you sort a vector v in descending order in R?
    a) sort(v, decreasing = TRUE)
    b) sort(v, TRUE)
    c) v[sort()]
    d) order(v, decreasing = TRUE)
  7. How can you filter rows in df where Age is greater than 30 and Salary is less than 50000?
    a) df[df$Age > 30 & df$Salary < 50000, ]
    b) df[Age > 30 & Salary < 50000]
    c) df[Age > 30 | Salary < 50000]
    d) df[Age > 30, Salary < 50000]
  8. How do you sort a data frame by the Age column and then by the Salary column in ascending order?
    a) df[order(df$Age, df$Salary), ]
    b) df[sort(df$Age, df$Salary), ]
    c) df[order(df$Age, df$Salary)]
    d) df[sort(df$Age + df$Salary)]
  9. Which R function would you use to filter a data frame based on multiple conditions?
    a) filter()
    b) subset()
    c) select()
    d) order()
  10. How do you filter rows of a data frame to exclude NA values in a column?
    a) df[!is.na(df$Age), ]
    b) df[is.na(df$Age), ]
    c) df[Age != NA, ]
    d) df[na.omit(df$Age), ]

3. Combining and Reshaping Data

  1. Which function in R is used to combine two data frames by rows?
    a) rbind()
    b) cbind()
    c) merge()
    d) concat()
  2. How do you combine two data frames df1 and df2 by columns in R?
    a) cbind(df1, df2)
    b) rbind(df1, df2)
    c) merge(df1, df2)
    d) concat(df1, df2)
  3. Which function is used to merge two data frames based on common columns in R?
    a) merge()
    b) rbind()
    c) cbind()
    d) join()
  4. What is the result of rbind(df1, df2) in R?
    a) Combines df1 and df2 by adding rows
    b) Combines df1 and df2 by adding columns
    c) Joins df1 and df2 by a common key
    d) Transposes df1 and df2
  5. Which function in R is used to reshape data by pivoting or melting?
    a) reshape()
    b) melt()
    c) spread()
    d) pivot()
  6. How do you convert a data frame from wide format to long format in R?
    a) melt()
    b) reshape()
    c) spread()
    d) pivot_longer()
  7. What does the pivot_wider() function do in R?
    a) Converts long-format data into wide-format data
    b) Converts wide-format data into long-format data
    c) Reshapes the data to include more columns
    d) Merges two data frames by a key
  8. How do you reshape a data frame from long format to wide format in R?
    a) pivot_wider()
    b) melt()
    c) spread()
    d) reshape()
  9. What does the cbind() function do in R?
    a) Combines data frames by columns
    b) Combines data frames by rows
    c) Merges data frames by common keys
    d) Creates a matrix
  10. Which function can be used to convert a data frame into a matrix in R?
    a) data.matrix()
    b) as.matrix()
    c) matrix()
    d) df_to_matrix()

Answers

QnoAnswer
1a) vector[]
2a) v[2]
3a) The first three rows of df
4a) df[df$Age > 30, ]
5a) df[, 3]
6a) It prevents R from dropping dimensions when selecting a single row or column
7a) df[df$X == 5, ]
8a) It subsets a data frame based on a condition
9a) df[, c(1, 2)]
10a) Removes the first column
11a) filter()
12a) df[order(df$Age), ]
13b) sort()
14a) df[order(df$Age, df$Salary), ]
15a) Sorts data in ascending or descending order
16a) sort(v, decreasing = TRUE)
17a) df[df$Age > 30 & df$Salary < 50000, ]
18a) df[order(df$Age, df$Salary), ]
19a) filter()
20a) df[!is.na(df$Age), ]
21a) rbind()
22a) cbind(df1, df2)
23a) merge()
24a) Combines df1 and df2 by adding rows
25b) melt()
26a) melt()
27a) Converts long-format data into wide-format data
28a) pivot_wider()
29a) Combines data frames by columns
30b) as.matrix()

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top