Interactive Subsetting Exercises

July 29, 2016
By

(This article was first published on R-exercises, and kindly contributed to R-bloggers)

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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Sponsors

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)