subset() function in R Language is used to create subsets of a Data frame. This can also be used to drop columns from a data frame.
Syntax: subset(df, expr)
Parameters:
df: Data frame used
expr: Condition for subset
Example 1:
# R program to create # subset of a data frame # Creating a Data Frame df<-data.frame(row1 = 0:2, row2 = 3:5, row3 = 6:8) print ("Original Data Frame") print (df) # Creating a Subset df1<-subset(df, select = row2) print("Modified Data Frame") print(df1) |
Output:
[1] "Original Data Frame" row1 row2 row3 1 0 3 6 2 1 4 7 3 2 5 8 [1] "Modified Data Frame" row2 1 3 2 4 3 5
Here, in the above code, the original data frame remains intact while another subset of data frame is created which holds selected row from the original data frame.
Example 2:
# R program to create # subset of a data frame # Creating a Data Frame df<-data.frame(row1 = 0:2, row2 = 3:5, row3 = 6:8) print ("Original Data Frame") print (df) # Creating a Subset df<-subset(df, select = -c(row2, row3)) print("Modified Data Frame") print(df) |
Output:
[1] "Original Data Frame" row1 row2 row3 1 0 3 6 2 1 4 7 3 2 5 8 [1] "Modified Data Frame" row1 1 0 2 1 3 2
Here, in the above code, the rows are permanently deleted from the original data frame.
Recommended Posts:
- Convert an Object to Data Frame in R Programming - as.data.frame() Function
- Check if the Object is a Data Frame in R Programming - is.data.frame() Function
- Create a Data Frame of all the Combinations of Vectors passed as Argument in R Programming - expand.grid() Function
- Create Matrix and Data Frame from Lists in R Programming
- Convert a Data Frame into a Numeric Matrix in R Programming - data.matrix() Function
- Modify Data of a Data Frame with an Expression in R Programming - with() Function
- Accessing variables of a data frame in R Programming - attach() and detach() function
- Get Transpose of a Matrix or Data Frame in R Programming - t() Function
- Convert a Data Frame into a Molten Form in R Programming - melt() Function
- Rename Columns of a Data Frame in R Programming - rename() Function
- Choose Specific Columns of a Data Frame in R Programming - select() Function
- Add new Variables to a Data Frame using Existing Variables in R Programming - mutate() Function
- Condense Column Values of a Data Frame in R Programming - summarise() Function
- Take Random Samples from a Data Frame in R Programming - sample_n() Function
- Creating a Data Frame from Vectors in R Programming
- Modify values of a Data Frame in R Language - transform() Function
- Binding rows and columns of a Data Frame in R - bind_rows() and bind_cols() Function
- Create Quantiles of a Data Set in R Programming - quantile() Function
- Create a Tabular representation of Data in R Programming - table() Function
- Compute Summary Statistics of Subsets in R Programming - aggregate() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

