The “filter” Function in R

  • Package: dplyr

  • Purpose: Filters rows of data frames based on specified conditions.

  • General Class: Data Manipulation

  • Required Argument(s):

    • .data: A data frame or tibble.

    • ...: Conditions specifying the rows to keep.

  • Notable Optional Arguments:

    • None specific to filter, but other functions from dplyr can be used in combination, e.g., select, mutate, etc.

  • Example:

  • # Example data for filtering a data frame using dplyr
    library(dplyr)

    df <- data.frame(
    ID = 1:5,
    Name = c("Alice", "Bob", "Charlie", "David", "Emma"),
    Age = c(25, 30, 22, 28, 35)
    )

    # Filter data frame to include only individuals with age greater than 25
    filtered_df <- filter(df, Age > 25)

    # Display the filtered data frame
    print(filtered_df)

  • In this example, the filter function from the dplyr package is used to extract a subset of a data frame (df) where the condition is that the “Age” column is greater than 25. The result is a filtered data frame (filtered_df) containing only the rows that meet the specified condition. Note that you need to have the dplyr package installed and loaded to use the filter function.

Previous
Previous

The “select” Function in R

Next
Next

The “chisq.test” Function in R