Create Subsets of a Data frame in R Programming - subset() Function Last Updated : 30 Apr, 2025 Comments Improve Suggest changes Like Article Like Report subset() function in R Programming 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 usedexpr: Condition for subsetCreate Subsets of Data Frames in R Programming LanguageHere we will make subsets of dataframe using subset() methods in R language.Example 1: Basic example of R - subset() FunctionWe are creating a data frame df with three rows and columns. Then, we use subset() to select only the row2 column and store it in df1. Finally, we print both the original and modified data frames. R df<-data.frame(row1 = 0:2, row2 = 3:5, row3 = 6:8) print ("Original Data Frame") print (df) df1<-subset(df, select = row2) print("Modified Data Frame") print(df1) Output: Create Subsets of a Data frame Example 2: Create Subsets of Data frameWe are creating a data frame df with three columns (row1, row2 and row3). Then, we use the subset() function to remove the row2 and row3 columns from the data frame. The modified data frame, containing only row1, is printed after the operation. R df<-data.frame(row1 = 0:2, row2 = 3:5, row3 = 6:8) print("Original Data Frame") print(df) df<-subset(df, select = -c(row2, row3)) print("Modified Data Frame") print(df) Output: Create Subsets of a Data frame Example 3: Logical AND and OR using subsetWe are creating a data frame df with three columns: ID, Name and Age. Then, we use the subset() function to create two new subsets:subset_df filters the data where Age is greater than 25 and ID is less than 4.subset_df2 filters the data where Age is greater than 30 or ID is equal to 2. R df <- data.frame( ID = 1:5, Name = c("Nishant", "Vipul", "Jayesh", "Abhishek", "Shivang"), Age = c(25, 30, 22, 35, 28) ) print("Original Dataframe") head(df) subset_df <- subset(df, subset = Age > 25 & ID < 4) subset_df2 <- subset(df, subset = Age > 30 | ID == 2) print("Subset 1") head(subset_df) print("Subset 2") head(subset_df2) Output:Create Subsets of a Data frame Example 4: Subsetting with Missing ValuesWe are creating a data frame df with three columns: ID, Name and Age, where some values are missing (NA). Then, we use the subset() function to filter out rows where the Age column has missing values (NA). The resulting data frame, which contains only rows with non-missing Age values, is printed. R df <- data.frame( ID = 1:5, Name = c("Nishant", "Vipul", NA, "Abhishek", NA), Age = c(25, 30, NA, 35, NA) ) print("Original Datafame") head(df) subset_df <- subset(df, subset = !is.na(Age)) print("Resultant Dataframe") head(subset_df) Output:Create Subsets of a Data frame In this article, we explored how to create subsets of a data frame in R using the subset() function. We also demonstrated how to filter rows based on conditions, select specific columns and handle missing values. Comment More infoAdvertise with us Next Article Create Subsets of a Data frame in R Programming - subset() Function N nidhi_biet Follow Improve Article Tags : R Language R DataFrame-Function Similar Reads R Tutorial | Learn R Programming Language R is an interpreted programming language widely used for statistical computing, data analysis and visualization. R language is open-source with large community support. R provides structured approach to data manipulation, along with decent libraries and packages like Dplyr, Ggplot2, shiny, Janitor a 4 min read R Programming Language - Introduction R is a programming language and software environment that has become the first choice for statistical computing and data analysis. Developed in the early 1990s by Ross Ihaka and Robert Gentleman, R was built to simplify complex data manipulation and create clear, customizable visualizations. Over ti 4 min read R-Data Frames R Programming Language is an open-source programming language that is widely used as a statistical software and data analysis tool. Data Frames in R Language are generic data objects of R that are used to store tabular data. Data frames can also be interpreted as matrices where each column of a matr 6 min read Read contents of a CSV File in R Programming - read.csv() Function read.csv() function in R Language is used to read "comma separated value" files. It imports data in the form of a data frame. The read.csv() function also accepts a number of optional arguments that we can use to modify the import procedure. we can choose to treat the first row as column names, sele 3 min read R-Data Types Data types in R define the kind of values that variables can hold. Choosing the right data type helps optimize memory usage and computation. Unlike some languages, R does not require explicit data type declarations while variables can change their type dynamically during execution.R Programming lang 5 min read Data Visualization in R Data visualization is the practice of representing data through visual elements like graphs, charts, and maps. It helps in understanding large datasets more easily, making it possible to identify patterns and trends that support better decision-making. R is a language designed for statistical analys 5 min read R-Matrices R-matrix is a two-dimensional arrangement of data in rows and columns. In a matrix, rows are the ones that run horizontally and columns are the ones that run vertically. In R programming, matrices are two-dimensional, homogeneous data structures. These are some examples of matrices:R - MatricesCreat 10 min read apply(), lapply(), sapply(), and tapply() in R In this article, we will learn about the apply(), lapply(), sapply(), and tapply() functions in the R Programming Language. The apply() collection is a part of R essential package. This family of functions helps us to apply a certain function to a certain data frame, list, or vector and return the r 4 min read Functions in R Programming A function accepts input arguments and produces the output by executing valid R commands that are inside the function. Functions are useful when we want to perform a certain task multiple times.In R Programming Language when we are creating a function the function name and the file in which we are c 5 min read R - Bar Charts Bar charts provide an easy method of representing categorical data in the form of bars. The length or height of each bar represents the value of the category it represents. In R, bar charts are created using the function barplot(), and it can be applied both for vertical and horizontal charts.Syntax 4 min read Like