# Interactive Subsetting Exercises

July 29, 2016
By

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

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 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...