Method Using Index Slicing

This method is used when the analyst was aware of the row/ column numbers to extract from the main dataset and create a subset from them for easy analysis. The numbers given to those rows or columns are called Index(s).

Syntax: dataframe[rows,columns]

Example: To make a subset of the dataframe of the first five rows and the second and fourth column

R




subset_1<-student_details[c(1:5),c(2,4)]
print(subset_1)


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:

 

Similar Reads

Method 1. Using Index Slicing

...

Method 2. Using subset() function

This method is used when the analyst was aware of the row/ column numbers to extract from the main dataset and create a subset from them for easy analysis. The numbers given to those rows or columns are called Index(s)....

Method 3. Using dplyr package functions

...