Skip to content

Instantly share code, notes, and snippets.

@martin-ueding
Last active June 11, 2018 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martin-ueding/a33cde96a6418684fa39475d29198411 to your computer and use it in GitHub Desktop.
Save martin-ueding/a33cde96a6418684fa39475d29198411 to your computer and use it in GitHub Desktop.
#' Apply and Merge
#'
#' Applies the given function to each row of the data frame. The function is
#' expected to return a data frame itself. The resulting data frames are bound
#' together and joined with the previous data frame.
apply_merge <- function (df, f, cols) {
# We first need to label the rows in the given data frame. We use a column
# name with period as this is an implementation detail.
df$.id <- 1:nrow(df)
# The function gets applied to the given columns. Since the user has
# specified them as strings, we need to retrieve those from the given data
# frame.
call <- c(list(f), lapply(cols, function (col) df[[col]]), list(SIMPLIFY = FALSE))
applied <- do.call(mapply, call)
# Each of the resulting data frames need to be labelled with the index
# column as well in order to allow for a join later.
for (id in 1:nrow(df)) {
applied[[id]]$.id <- id
}
# All intermediate data frames are combined.
bound <- do.call(rbind, applied)
# We join these together with the orignal data frame.
joined <- dplyr::inner_join(df, bound, by = '.id')
# The index column is no longer needed and deleted.
joined$.id <- NULL
return(joined)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment