The subset() function returns a subset of a data frame that matches the specified conditions.


subset(df_object, expression)
  • The df_object parameter is the data frame to retrieve the subset from.
  • The expression specifies the subset conditions.


The following example uses a CSV file named inventory.csv with the following information in it:

Item Quantity Cost
Hoodies 58 25
Pens 80 1.5
Pencil 75 0.88
T-shirts 100 15

The subset() function can be used to get the subset of items where the Cost is greater than 10:

# Read file
df <- read.csv("inventory.csv")
# Retrieve subset
subset(df, Cost > 10)

The example returns the following:

Item Quantity Cost
1 Hoodies 58 25
4 T-shirts 100 15


Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn R on Codecademy