Built with Tutify.io

    AIStudyLabs

    R Select Rows by Condition with Examples

    R Select Rows by Condition with Examples

    How to select rows by the condition in the R data frame? You can select rows from the R dataframe using bracket notation by single/multiple conditions. In this article, I will explain how to select rows based on a list of values, by multiple and single conditions, not equal conditions, etc.

    <a id="h-quick-examples-of-select-rows-by-condition"></a>

    Quick Examples of Select Rows by Condition

    Below are quick examples of selecting rows by condition from the R data frame.

    
    # Quick Examples
    
    # Example 1: Select Rows by equal condition
    df[df$gender == 'M',]
    
    # Example 2: Select Rows by not equal condition
    df[df$gender != 'M',]
    
    # Example 3: Select Rows by Multiple Conditions
    df[df$gender == 'M' & df$id > 15,]
    
    # Example 4: Select rows based on list
    df[df$id %in% c(13,14,15),]
    
    # Example 5: Using subset()
    subset(df, gender == 'M')
    
    # Example 6: Using filter()
    library("dplyr")  
    filter(df, gender == 'M')
    

    Let's initiate an R data frame,

    
    # Create DataFrame
    df <- data.frame(
      id = c(10,11,12,13,14,15,16,17),
      name = c('sai','ram','deepika','sahithi','kumar','scott','Don','Lin'),
      gender = c('M','M','F','F','M','M','M','F'),
      dob = as.Date(c('1990-10-02','1981-3-24','1987-6-14','1985-8-16',
                      '1990-10-02','1981-3-24','1987-6-14','1985-8-16')),
      state = c('CA','NY',NA,NA,'DC','DW','AZ','PH')
    )
    df
    

    Yields below output.

    <a id="h-select-rows-based-on-the-condition"></a>

    Select Rows Based on the Condition

    By using bracket notation we can select rows by the condition in R. In the following example, I am selecting all rows where gender is equal to 'M' from DataFrame. For more examples refer to selecting rows from the data frame.

    
    # Select Rows by equal condition
    df[df$gender == 'M',]
    

    Yields below output.

    <a id="h-select-rows-based-on-negative-condition"></a>

    Select Rows Based on Negative Condition

    Similarly, the below example performs negation. here I am selecting all rows that are not equal to 'M'. Let's filter the specified rows along with the specified condition.

    
    # Select Rows by not equal condition
    df[df$gender != 'M',]
    
    # Output
    #  id    name gender        dob state
    #3 12 deepika      F 1987-06-14  <NA>
    #4 13 sahithi      F 1985-08-16  <NA>
    #8 17     Lin      F 1990-08-26    PH
    

    In this case, rows with 'gender' not equal to 'M' are selected, showcasing the application of the negation operator (!=).

    <a id="h-select-rows-based-on-multiple-conditions"></a>

    Select Rows Based on Multiple Conditions

    Let's see how to select rows based on multiple conditions in R. In the below example I am selecting rows when gender is equal to M and id is greater than 15. Here I am using & (AND) logical operator. It returns true when both conditions are true.

    Similarly, you can also use | (or), ! (not), &&, and || operators.

    
    # Select Rows by Multiple Conditions
    df[df$gender == 'M' & df$id > 15,]
    
    # Output
    #  id name gender        dob state
    # 7 16  Don      M 1986-03-24    AZ
    

    <a id="h-select-rows-based-on-a-list-of-values"></a>

    Select Rows Based on a List of Values

    If you have a vector of values and you want to select rows based on a list of values (vector values) in R, use the operator %in% . The below example returns rows that have id values 13,14 and 15.

    
    # Select rows based on list
    df[df$id %in% c(13,14,15),]
    
    # Output
    #  id    name gender        dob state
    #4 13 sahithi      F 1985-08-16  <NA>
    #5 14   kumar      M 1995-03-02    DC
    #6 15   scott      M 1991-06-21    DW
    

    <a id="h-by-using-subset"></a>

    By Using subset()

    R base also provides a subset() function that can be used to select rows based on the logical condition of a column.

    
    # Using subset()
    subset(df, gender == 'M')
    

    <a id="h-by-using-filter"></a>

    By using filter()

    Finally, you can select rows from the data frame by using the filter() function from the dplyr package. To utilize this package, start by installing it with install.packages("dplyr") and then load it using library("dplyr").

    
    # Load dplyr package
    library("dplyr")
      
    # Using filter()
    filter(df, gender == 'M')
    

    <a id="h-conclusion"></a>

    Conclusion

    In this article, I have explained examples of R selecting rows from a data frame by single condition, multiple conditions, by not-equal condition, and finally, selecting rows based on a list of values.

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References