0
0
R Programmingprogramming~10 mins

select() for column selection in R Programming - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to select the column 'mpg' from the mtcars dataset using dplyr's select().

R Programming
library(dplyr)
selected_data <- mtcars %>% select([1])
print(selected_data)
Drag options to blanks, or click blank then click option'
Acyl
Bmpg
Cgear
Dhp
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing a column name that is not in the dataset.
Using quotes around the column name inside select(), which is not needed in tidyverse syntax.
2fill in blank
medium

Complete the code to select columns 'mpg' and 'cyl' from mtcars using select() with the colon operator.

R Programming
library(dplyr)
selected_data <- mtcars %>% select([1])
print(selected_data)
Drag options to blanks, or click blank then click option'
Ampg:cyl:gear
Bmpg,cyl
Cmpg:cyl
Dmpg-cyl
Attempts:
3 left
💡 Hint
Common Mistakes
Using commas inside select() without c(), which is not correct in tidyverse select.
Using dashes or extra colons which are invalid syntax.
3fill in blank
hard

Fix the error in the code to select columns 'mpg' and 'hp' from mtcars using select().

R Programming
library(dplyr)
selected_data <- mtcars %>% select([1])
print(selected_data)
Drag options to blanks, or click blank then click option'
Ampg, hp
B"mpg", "hp"
Cc(mpg, hp)
Dmpg hp
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around column names inside select().
Using c() to combine columns inside select(), which is not needed.
4fill in blank
hard

Fill both blanks to select columns from 'mpg' to 'hp' and exclude 'cyl' from mtcars.

R Programming
library(dplyr)
selected_data <- mtcars %>% select([1]) %>% select(-[2])
print(selected_data)
Drag options to blanks, or click blank then click option'
Ampg:hp
Bcyl
Cgear
Dhp
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to exclude columns before selecting the range.
Not using the minus sign to exclude a column.
5fill in blank
hard

Fill all three blanks to select columns starting with 'd' and ending with 'q' and exclude 'drat' from mtcars.

R Programming
library(dplyr)
selected_data <- mtcars %>% select(starts_with([1]), ends_with([2])) %>% select(-[3])
print(selected_data)
Drag options to blanks, or click blank then click option'
A"d"
B"q"
Cdrat
Dgear
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes inside starts_with or ends_with.
Trying to exclude columns without the minus sign.