The “split” Function in R

  • Package: Base R (no specific package required)

  • Purpose: Splits a vector, data frame, or list into groups based on a factor or list of factors.

  • General Class: Data Manipulation

  • Required Argument(s):

    • x: A vector, data frame, or list to be split.

    • f: A factor or a list of factors indicating how to split x.

  • Notable Optional Arguments:

    • drop: Logical; if TRUE, drop levels with zero counts.

    • ...: Additional arguments passed to split.default.

  • Example:

  • # Example data for using the split function
    values <- c(1, 2, 3, 4, 5, 6)
    groups <- factor(c("A", "B", "A", "B", "A", "B"))

    # Use split to create a list of vectors based on the factor 'groups'
    result <- split(x = values, f = groups)

    # Display the result
    print(result)

  • In this example, the split function is used to split the values vector into groups based on the groups factor. The resulting result list contains two vectors, one for each level of the factor. The split function is frequently used when you want to split data into groups based on factors for further analysis or processing.

Previous
Previous

The “unsplit” Function in R

Next
Next

The “replicate” Function in R