groupApply: Apply a function to groups of data in an Andromeda table

View source: R/Operations.R

groupApplyR Documentation

Apply a function to groups of data in an Andromeda table

Description

Apply a function to groups of data in an Andromeda table

Usage

groupApply(
  tbl,
  groupVariable,
  fun,
  ...,
  batchSize = 1e+05,
  progressBar = FALSE,
  safe = FALSE
)

Arguments

tbl

An Andromeda table (or any other 'DBI' table).

groupVariable

The variable to group by

fun

A function where the first argument is a data frame.

...

Additional parameters passed to fun.

batchSize

Number of rows fetched from the table at a time. This is not the number of rows to which the function will be applied. Included mostly for testing purposes.

progressBar

Show a progress bar?

safe

Create a copy of tbl first? Allows writing to the same Andromeda as being read from.

Details

This function applies a function to groups of data. The groups are identified by unique values of the groupVariable, which must be a variable in the table.

Value

Invisibly returns a list of objects, where each object is the output of the user-supplied function applied to a group.

See Also

batchApply()

Examples

andr <- andromeda(cars = cars)

fun <- function(x) {
  return(tibble::tibble(speed = x$speed[1], meanDist = mean(x$dist)))
}

result <- groupApply(andr$cars, "speed", fun)
result <- bind_rows(result)
result
# # A tibble: 19 x 2 
# speed meanDist 
# <dbl> <dbl> 
# 1 4 6 
# 2 7 13 
# 3 8 16 
# ...

close(andr)


Andromeda documentation built on Nov. 24, 2023, 5:10 p.m.