Skip to content

Instantly share code, notes, and snippets.

@jrosell
Created March 11, 2022 15:15
Show Gist options
  • Save jrosell/b95918ae70dfad3d2abb8357802ae01a to your computer and use it in GitHub Desktop.
Save jrosell/b95918ae70dfad3d2abb8357802ae01a to your computer and use it in GitHub Desktop.
#' Execute f for each row of df
#'
#' @param df data.frame
#' @param f function
#' @return A data.frame with the result of executin g for each row of df.
#' @examples
#' row_split_map(
#' tibble(x = c(1, 2), y = c(2, 3)),
#' function(df) {
#' df %>% mutate(
#' sum = df$x[[1]] + df$y[[1]]
#' )
#' }
#' )
row_split_map <- function(df, f) {
df %>%
group_by(.row_split_map_id = row_number()) %>%
group_split() %>%
map_dfr(f) %>%
select(-.row_split_map_id)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment