Learn R Programming

purrr (version 0.1.0)

by_slice: Apply a function to slices of a data frame

Description

by_slice() applies `..f` on each group of a data frame. Groups should be set with slice_rows() or group_by().

Usage

by_slice(.d, ..f, ..., .labels = TRUE)

Arguments

.d
A sliced data frame.
..f
A function to apply to each slice. If ..f does not return a data frame or an atomic vector, a list-column is created under the name .out. If it returns a data frame, it should have the same number of rows within groups and the sa
...
Further arguments passed to ..f.
.labels
If TRUE, the returned data frame is prepended with the labels of the slices (the columns in .d used to define the slices). They are recycled to match the output size in each slice if necessary.

Value

  • A data frame.

Details

by_slice() provide equivalent functionality to dplyr's do() function. In combination with map(), by_slice() is equivalent to summarise_each() and mutate_each(). The distinction between mutating and summarising operations is not as important as in dplyr because we do not act on the columns separately. When we map a function to each column, the only constraint is that the number of returned rows match inside each slice.

See Also

by_row(), slice_rows()

Examples

Run this code
# Here we fit a regression model inside each slice defined by the
# unique values of the column "cyl". The fitted models are returned
# in a list-column.
mtcars %>%
  slice_rows("cyl") %>%
  by_slice(partial(lm, mpg ~ disp))

# by_slice() is especially useful in combination with map().
# Mutating and summarising operations can be used indistinctly.

# Mutating operation:
mtcars %>%
  slice_rows(c("cyl", "am")) %>%
  by_slice(map, ~ .x / sd(.x))

# Summarising operation:
mtcars %>%
  slice_rows(c("cyl", "am")) %>%
  by_slice(map, mean)

# If you don't need the slicing variables as identifiers, switch
# .labels to FALSE:
mtcars %>%
  slice_rows("cyl") %>%
  by_slice(partial(lm, mpg ~ disp), .labels = FALSE) %>%
  flatten() %>%
  map(coef)

Run the code above in your browser using DataLab