Method Using subset() function
When the analyst is aware of row names and column names then subset() method is used. Simply, This function is used when we want to derive a subset of a dataframe based on implanting some conditions on rows and columns of the dataframe. This method is more efficient and easy to use than the Index method.
Syntax: subset(dataframe,rows_condition,column_condition)
Example: Extract names of students belonging to section1
R
subset_2= subset (student_details,section==1,stud_name) print (subset_2) |
Output:
How to select a subset of DataFrame in R
In general, when we were working on larger dataframes, we will be only interested in a small portion of it for analyzing it instead of considering all the rows and columns present in the dataframe.
Creation of Sample Dataset
Let’s create a sample dataframe of Students as follows
R
student_details < - data.frame ( stud_id= c (1: 10), stud_name= c ( "Anu" , "Abhi" , "Bob" , "Charan" , "Chandu" , "Daniel" , "Girish" , "Harish" , "Pandit" , "Suchith" ), age= c (18, 19, 17, 18, 19, 15, 21, 16, 15, 17), section= c (1, 2, 1, 2, 1, 1, 2, 1, 2, 1) ) print (student_details) |
Output: