Subsetting in R is a useful indexing feature for accessing object elements. It can be used to select and filter variables and observations. You can use brackets to select rows and columns from your dataframe.
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
Let’s quickly do one example how subset works and will explore more on this concept in Dataframes Topic
Here data1 is Variable to create dataframe, and three objects are assigned to this variable using data frame.
P will display columns from 1 to 3, q will display columns from 4 to 6 and r will display columns from 7 to 9.
Data.frame():
The function data. frame () creates data frames, tightly coupled collections of variables which share many of the properties of matrices and of lists, used as the fundamental data structure by most of R’s modeling software.
Data1 will print:
p q r
Row1 1 4 7
Row2 2 5 8
Row3 3 6 9
Now data2 variable prints the subset of data1 and selected q column
data2 displays:
q
Row1 4
Row2 5
Row3 6
Note: 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.
If I don’t require columns q & r:
This syntax written in line 10 prints only Column p