Interactive Subsetting Exercises

[This article was first published on R-exercises, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

box-24557_960_720The 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.

To leave a comment for the author, please follow the link and comment on their blog: R-exercises.

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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)