The “subset” Function in R

  • Package: Base R (no specific package required)

  • Purpose: Subsets a data frame or vector based on specified conditions.

  • General Class: Data Manipulation

  • Required Argument(s):

    • x: The data frame or vector to be subsetted.

    • subset: A logical expression indicating the conditions for subsetting.

  • Notable Optional Arguments:

    • select: A vector specifying which columns to select from the data frame.

    • drop: A logical value indicating whether to drop unused levels from factors.

  • Example:

  • # Example data frame
    data_df <- data.frame(ID = 1:5, Name = c("Alice", "Bob", "Charlie", "David", "Eva"), Age = c(25, 30, 22, 28, 35))

    # Subset data frame to include only rows where Age is greater than 25
    subset_df <- subset(data_df, Age > 25)

    # Display the subsetted data frame
    print(subset_df)

  • In this example, the subset function is used to subset a data frame (data_df) to include only rows where the “Age” column is greater than 25. The result is a subsetted data frame (subset_df) containing only the rows that satisfy the specified condition.

Previous
Previous

The “length” Function in R

Next
Next

The “merge” Function in R