Interactive Subsetting Exercises
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
The function, “subset()
” is intended as a convienent, interactive substitute for subsetting with brackets. subset()
extracts subsets of matrices, data frames, or vectors (including lists), according to specified conditions.
Answers to the exercises are available here.
Exercise 1
Subset the vector, “mtcars[,1]
“, for values greater than “15.0
“.
Exercise 2
Subset the dataframe, “mtcars
” for rows with “mpg
” greater than , or equal to, 21 miles per gallon.
Exercise 3
Subset “mtcars
” for rows wih “cyl
” less than “6
“, and “gear
” exactly equal to “4
“.
Exercise 4
Subset “mtcars
” for rows greater than, or equal to, 21 miles per gallon. Also, select only the columns, “mpg
” through “hp
“.
Exercise 5
Subset “airquality
” for “Ozone
” greater than “28
“, or “Temp
” greater than “70
“. Return the first five rows.
Exercise 6
Subset “airquality
” for “Ozone
” greater than “28
“, and “Temp
” greater than “70
“. Select the columns, “Ozone
” and “Temp
“. Return the first five rows.
Exercise 7
Subset the “CO2
” dataframe for “Treatment
” values of “chilled
“,
and “uptake
” values greater that “15
“. Remove the category, “conc
“. Return the first 10 rows.
Exercise 8
Subset the “airquality
” dataframe for rows without “Ozone
” values of “NA
“.
Exercise 9
Subset “airquality
” for “Ozone
” greater than “100
“. Select the columns “Ozone
“, “Temp
“, “Month
” and “Day
” only.
Exercise 10
Subset “LifeCycleSavings
” for “sr
” greater than “8
“, and less than “10
“. Remove columns “pop75
” through “dpi
“.
Image by Clker-free-vector-images (Pixabay post) [CC0 Public Domain ], via Pixabay.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.