Apply a function to groups of data in an Andromeda table
groupApply(
tbl,
groupVariable,
fun,
...,
batchSize = 1e+05,
progressBar = FALSE,
safe = FALSE
)
An Andromeda
table (or any other 'DBI' table).
The variable to group by
A function where the first argument is a data frame.
Additional parameters passed to fun.
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.
Show a progress bar?
Create a copy of tbl
first? Allows writing to the same Andromeda as being
read from.
Invisibly returns a list of objects, where each object is the output of the user-supplied function applied to a group.
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.
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 × 2
#> speed meanDist
#> <dbl> <dbl>
#> 1 4 6
#> 2 7 13
#> 3 8 16
#> 4 9 10
#> 5 10 26
#> 6 11 22.5
#> 7 12 21.5
#> 8 13 35
#> 9 14 50.5
#> 10 15 33.3
#> 11 16 36
#> 12 17 40.7
#> 13 18 64.5
#> 14 19 50
#> 15 20 50.4
#> 16 22 66
#> 17 23 54
#> 18 24 93.8
#> 19 25 85
# # A tibble: 19 x 2
# speed meanDist
# <dbl> <dbl>
# 1 4 6
# 2 7 13
# 3 8 16
# ...
close(andr)