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
11
votes
2 answers

Where is the Purrr ~ operator documented?

I searched for ??"~" but this only points me to rlang::env_bind (presumably, %<~%) and base::~. Within RStudio, how can I find Purrr's ~'s documentation? For example, if I forgot how to use ~ with two inputs, where do I look?
J. Mini
  • 1,868
  • 1
  • 9
  • 38
11
votes
1 answer

How to locate errors and debug when using purrr

I find it difficult to debug my code when using purrr and some of the map() variants. Especially I have problems locating where my code fails because the error messages do not tell me which row (dataframe) threw the error. What is a good approach…
Steen Harsted
  • 1,802
  • 2
  • 21
  • 34
11
votes
1 answer

Date column coerced to numeric when indexing dataframe with [[ and a vector

I am creating a data.frame with a column of type Date. When indexing the data frame with [[ and a numeric vector, the Date becomes a number. This is causing a problem when using purrr::pmap. Can anyone explain why this is happening and is there a…
MrHopko
  • 879
  • 1
  • 7
  • 16
11
votes
2 answers

Harnessing .f list names with purrr::pmap

The following works ok: pmap_dbl(iris, ~ ..1 + ..2 + ..3 + ..4) The documentation for .l provides for A list of lists. ... List names will be used if present.. This suggests you should be able to work with the list names (i.e. column names).…
geotheory
  • 22,624
  • 29
  • 119
  • 196
11
votes
1 answer

Clarity on purrr syntax

I often find myself making incorrect choices in variables names when using purrr. For example, take the code on the github page of purrr. library(purrr) mtcars %>% split(.$cyl) in split(.$cyl) I often make the mistake of using split(cyl). This…
Alex
  • 2,603
  • 4
  • 40
  • 73
11
votes
2 answers

Get the name of a list item created with purrr::map

I retrieved a list of csv files with purrr::map and got a large list. csv_files <- list.files(path = data_path, pattern = '\\.csv$', full.names = TRUE) all_csv <- purrr::map(csv_files, readr::read_csv2) names(all_csv) <- gsub(data_path, "",…
Yann
  • 887
  • 4
  • 12
  • 20
10
votes
1 answer

Use of glue in map in an RMarkdown in a new environment

Consider the following Rmarkdown document: --- title: "Environments" author: "Me" date: "2023-01-13" output: html_document --- ```{r setup} library(glue) library(purrr) ``` ```{r vars} a <- 1 x <- list("`a` has the value: {a}") ``` ```{r…
thothal
  • 16,690
  • 3
  • 36
  • 71
10
votes
3 answers

R bootstrap regression with facet_wrap

Been practicing with the mtcars dataset. I created this graph with a linear model. library(tidyverse) library(tidymodels) ggplot(data = mtcars, aes(x = wt, y = mpg)) + geom_point() + geom_smooth(method = 'lm') Then I converted the dataframe to…
hachiko
  • 671
  • 7
  • 20
10
votes
3 answers

What's a tidyverse approach to iterating over rows in a data frame when vectorisation is not feasible?

I want to know the best way to iterate over rows of a data frame when the value of a variable at row n depends on the value of variable(s) at row n-1 and/or n-2. Ideally I would like to do this in a "tidyverse" way, perhaps with purrr::pmap(). For…
Matt Cowgill
  • 659
  • 4
  • 13
10
votes
5 answers

Safer purrr::map2 for lists with names out of order

This is a question for which I've written failsafes in my code before, but I'm wondering if there's something more straightforward that I've missed. I sometimes have 2 (or more) lists that contain different types of information that need to work…
camille
  • 16,432
  • 18
  • 38
  • 60
10
votes
2 answers

how to "spread" a list-column?

Consider this simple example mydf <- data_frame(regular_col = c(1,2), normal_col = c('a','b'), weird_col = list(list('hakuna', 'matata'), list('squash', 'banana'))) > mydf # A…
ℕʘʘḆḽḘ
  • 18,566
  • 34
  • 128
  • 235
10
votes
4 answers

rolling regression by group in the tidyverse?

There are many questions about rolling regression in R, but here I am specifically looking for something that uses dplyr, broom and (if needed) purrr. This is what makes this question different. I want to be tidyverse consistent. Is is possible to…
ℕʘʘḆḽḘ
  • 18,566
  • 34
  • 128
  • 235
10
votes
2 answers

How to pass second parameter to function while using the map function of purrr package in R

Apologies for what might be a very simple question. I am new to using the purrr package in R and I'm struggling with trying to pass a second parameter to a function. library(dplyr) library(purrr) my_function <- function(x, y = 2) { z = x + y …
elvikingo
  • 947
  • 1
  • 11
  • 20
10
votes
3 answers

How to loop over a tidy eval function using purrr?

I have the following data set (sample): train <- data.frame(ps_ind_06_bin = c(FALSE, FALSE, FALSE, TRUE, TRUE, FALSE), ps_ind_07_bin = c(FALSE, TRUE, TRUE, FALSE, TRUE, TRUE), ps_ind_08_bin = c(TRUE,…
Ramiro Bentes
  • 338
  • 1
  • 9
10
votes
4 answers

R: Extract columns from list of data.frames in a tibble

I am wondering how to manipulate a list containing data.frames stored in a tibble. Specifically, I would like to extract two columns from a data.frame that are stored in a tibble list column. I would like to go from this tibble c …
Iain
  • 1,608
  • 4
  • 22
  • 27