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.

**What's next:**

- Explore all our (>1000) R exercises
- Find an R course using our R Course Finder directory
- Subscribe to receive weekly updates and bonus sets by email
- Share with your friends and colleagues using the buttons below

Dr Pankaj Kumar Agarwal says

Why the answer is different if Exercise 6 is done as follows:

airquality[airquality$Ozone>28&airquality$Temp>70,][1:5,]

John Akwei says

Hi Dr. Agarwal,

You forgot, in Exercise 6, “Select the columns, “Ozone” and “Temp“”.

The entire lesson emphasizes interactivity, rather than R for programmatic scripting.

However, if you were trying to code the specifications in Exercise 6 for a program, your suggestion would work, with column specifications included.

Here is the code:

airquality[airquality$Ozone>28&airquality$Temp>70,][1:5,c(1,4)]