Skip to content

Instantly share code, notes, and snippets.

@brshallo
Created February 19, 2019 15:26
Show Gist options
  • Save brshallo/4cf343686e92acc4f57cda7aa9b7f331 to your computer and use it in GitHub Desktop.
Save brshallo/4cf343686e92acc4f57cda7aa9b7f331 to your computer and use it in GitHub Desktop.
Function to add multiple inputs
library(purrr)
add_vecs <- function(...){
if(!is.list(...)) stop("must be list input")
reduce(..., `+`)
}
add_vecs(list(1:5, 1:5, 1:5))
#> [1] 3 6 9 12 15
<sup>Created on 2019-02-15 by the [reprex package](https://reprex.tidyverse.org) (v0.2.1)</sup>
# Apply to dataframe
iris %>%
mutate(sum_lengths = select(., contains("Length")) %>% add_vecs())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment