Questions tagged [purrr]

purrr enhances R’s functional programming toolkit by providing a complete and consistent set of tools for working with functions and vectors, such as map() and reduce().

purrr enhances R’s functional programming toolkit by providing a complete and consistent set of tools for working with functions and vectors, such as map() and reduce().

Official CRAN Documentation

https://cran.r-project.org/web/packages/purrr/index.html

Online Resources

Source Code

https://github.com/hadley/purrr

3401 questions
16
votes
3 answers

Pass multiple functions to purrr:map

I would like to pass multiple functions at once to one purrr::map call, where the functions need some arguments. As pseudo code: funs <- c(median, mean) mtcars %>% purrr::map(funs, na.rm = TRUE) This code does not run, but is intended to show…
Sebastian Sauer
  • 1,555
  • 15
  • 24
16
votes
2 answers

R - Parallelizing multiple model learning (with dplyr and purrr)

This is a follow up to a previous question about learning multiple models. The use case is that I have multiple observations for each subject, and I want to train a model for each of them. See Hadley's excellent presentation on how to do this. In…
Bar
  • 2,736
  • 3
  • 33
  • 41
15
votes
1 answer

Use filter() (and other dplyr functions) inside nested data frames with map()

I'm trying to use map() of purrr package to apply filter() function to the data stored in a nested data frame. "Why wouldn't you filter first, and then nest? - you might ask. That will work (and I'll show my desired outcome using such process), but…
Taraas
  • 1,268
  • 1
  • 11
  • 20
15
votes
4 answers

Using purrr::pmap within mutate to create list-column

I understand how to use map to iterate over arguments in a df and create a new list column. For example, params <- expand.grid(param_a = c(2, 4, 6) ,param_b = c(3, 6, 9) ,param_c = c(50, 100) …
matsuo_basho
  • 2,833
  • 8
  • 26
  • 47
14
votes
2 answers

Equivalent of `break` in purrr::map

Say I want to run a loop until a condition is met, at which point the result is saved and the loop exits: library(tidyverse) for (i in 1:5) { df <- iris %>% select(i) %>% head(2) if (names(df) == "Petal.Width") { out <- df break …
Shinobi_Atobe
  • 1,793
  • 1
  • 18
  • 35
14
votes
1 answer

Error in bind_rows_(x, .id) : Argument 1 must have names using map_df in purrr

I'm using the spotifyr package to scrape spotify audio features for every song of specific albums in my dataset. My issue is that my dataset consists of some artists that are not on spotify -- so they shouldn't be returning any values. My issue is…
Evan O.
  • 1,553
  • 2
  • 11
  • 20
14
votes
2 answers

Create a column based on the name of the element list that contain the data frame in R

I have a list of data frames and the names of the element list contains information about each data frame. Here is a reproducible example, list_df <- list(jan_2013 = data.frame(id = 1:10, x = rnorm(10), y = runif(10)), feb_2013 =…
Cristóbal Alcázar
  • 1,153
  • 14
  • 26
14
votes
1 answer

Map function to second level of nested list using purrr

I'm trying to better understand functional programming in R. I'd like to stick to purrr, but I'll use rapply to demonstrate what I'm looking for below. First, a simple example of what I'm trying to understand: You can use map to get the mean of each…
twgardner2
  • 630
  • 1
  • 8
  • 27
14
votes
1 answer

Equivalent of rowwise() do() with purrr, now that by_row() is deprecated?

Now that by_row() in purrr is going to be (is?) deprecated, what is the new preferred tidyverse implementation of: somedata = expand.grid(a=1:3,b=3,c=runif(3)) somedata %>% rowwise() %>% do(binom.test(x=.$a,n=.$b,p=.$c) %>% tidy()) It seems as if…
Nicholas Root
  • 535
  • 3
  • 15
14
votes
2 answers

Add new variable to list of data frames with purrr and mutate() from dplyr

I know that there are many related questions here on SO, but I am looking for a purrr solution, please, not one from the apply list of functions or cbind/rbdind (I want to take this opportunity to get to know purrr better). I have a list of…
RobertMyles
  • 2,673
  • 3
  • 30
  • 45
13
votes
2 answers

How to use walk to silently plot ggplot2 output with purrr

I am trying to understand how to use walk to silently (without printing to the console) return ggplot2 plots in a pipeline. library(tidyverse) # EX1: This works, but prints [[1]], [[2]], ..., [[10]] to the console 10 %>% rerun(x = rnorm(5), y =…
JasonAizkalns
  • 20,243
  • 8
  • 57
  • 116
13
votes
4 answers

`purrr::map` to any type

Is there a way to map to any type with purrr::map library(tidyverse) library(lubridate) df <- data_frame(id = c(1, 1, 1, 2, 2, 2), val = c(1, 2, 3, 1, 2, 3), date = ymd("2017-01-01") + days(1:6)) df1 <- df %>%…
johannes
  • 14,043
  • 5
  • 40
  • 51
12
votes
4 answers

Sum amount last 6 month prior to the date of transaction

This is my transaction data. It shows the transactions made from the accounts in from column to the accounts in to column with the date and the amount information data id from to date amount
rlock
  • 133
  • 9
12
votes
5 answers

Map dplyr function to each combination of variable pairs in an R dataframe

I want to map a function to each combination pair of variables in a dataframe in R, returning a dataframe with the function output for each pair. I can do this manually like so: library(tidyverse) df <- tibble(a = c(1, 2), b = c(4, 3), c = c(5,…
cengstro
  • 260
  • 1
  • 9
12
votes
4 answers

can we iterate over two lists with purrr (not simultaneously)?

Consider that I have a function f(x,y) that takes two argument and two lists x_var = list('x','y','z') and y_var = list('a','b') Is there a purrr function that allows me to iterate every combination of one element in x_var and one element in…
ℕʘʘḆḽḘ
  • 18,566
  • 34
  • 128
  • 235