Complete the code to select the column 'mpg' from the mtcars dataset using dplyr's select().
library(dplyr) selected_data <- mtcars %>% select([1]) print(selected_data)
The select() function chooses columns by their names. Here, we want to select the 'mpg' column.
Complete the code to select columns 'mpg' and 'cyl' from mtcars using select() with the colon operator.
library(dplyr) selected_data <- mtcars %>% select([1]) print(selected_data)
The colon operator mpg:cyl selects all columns from 'mpg' to 'cyl' inclusive in their order in the dataset.
Fix the error in the code to select columns 'mpg' and 'hp' from mtcars using select().
library(dplyr) selected_data <- mtcars %>% select([1]) print(selected_data)
In select(), column names are unquoted and separated by commas without c(). So mpg, hp is correct.
Fill both blanks to select columns from 'mpg' to 'hp' and exclude 'cyl' from mtcars.
library(dplyr) selected_data <- mtcars %>% select([1]) %>% select(-[2]) print(selected_data)
First, select columns from 'mpg' to 'hp' using mpg:hp. Then exclude 'cyl' by using -cyl.
Fill all three blanks to select columns starting with 'd' and ending with 'q' and exclude 'drat' from mtcars.
library(dplyr) selected_data <- mtcars %>% select(starts_with([1]), ends_with([2])) %>% select(-[3]) print(selected_data)
Use starts_with("d") and ends_with("q") to select columns starting and ending with those letters. Then exclude 'drat' with -drat.