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
7
votes
1 answer

How to use .names with dplyr mutate across and an anonymous function

Referring to the documentation for across() (https://dplyr.tidyverse.org/reference/across.html), you can specify the name returned from the dplyr verb using the .names argument. Viz iris %>% group_by(Species) %>% …
RDavey
  • 1,530
  • 10
  • 27
7
votes
2 answers

how to unquote (!!) inside `map` inside `mutate`

I'm modifying nested data frames inside of foo with map2 and mutate, and I'd like to name a variable in each nested data frame according to foo$name. I'm not sure what the proper syntax for nse/tidyeval unquotation would be here. My…
lost
  • 1,483
  • 1
  • 11
  • 19
7
votes
2 answers

How to speed up iteration while working on a data-frame with over 5 million observations in r?

I am trying to generate values for over 7 variables across millions of observations and it's taking forever when I write a for loop to achieve this. Below is an example of what I am trying to achieve. In this case it's fast since it has only a few…
Dal
  • 317
  • 1
  • 8
7
votes
2 answers

Counter in purrr's map* function family

I often need some sort of counter / index value when applying loop-functions to vectors / list. When using basic loop functions, this index can be created by successively adding 1 to some initial value. Consider the following example: lets <-…
Ratnanil
  • 1,641
  • 17
  • 43
7
votes
7 answers

How to turn a list inside out?

I've got a following list: > list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7)) [[1]] [1] 3 4 5 8 [[2]] [1] 2 6 9 10 [[3]] [1] 1 7 So we can say that 3 belongs to group 1, 6 belongs to group 2, 7 belongs to group 3 and so on. I need a reverse…
jakes
  • 1,964
  • 3
  • 18
  • 50
7
votes
2 answers

Unnest list and gather items with purrr

I have a list like: list(list(goals = c(42L, 52L, 55L), season = "88", player = c("a", "b","c")), list(goals = c(41L,53L, 37L, 40L), season = "89", player = c("a","b", "c", "d"))) I want to convert…
Domino55
  • 73
  • 3
7
votes
1 answer

use assign() inside purrr:walk()

I have a number of dataframes and a series of changes I want to make to each of them. For this example, let to the desired change be simply making each data frame a tibble using as_tibble. I know there are various ways of doing this, but I'd like to…
lost
  • 1,483
  • 1
  • 11
  • 19
7
votes
1 answer

Function to extract all list elements from a dataframe column into individual columns

After using purrr:map along a time series list column, I end up with the results in a tibble list (I may have some terminology wrong here, but hopefully the example will clear things up). Is it possible to extract each column in the resulting list…
user1420372
  • 2,077
  • 3
  • 25
  • 42
7
votes
2 answers

when to use map() function and when to use summarise_at()/mutate_at()

Can anyone give a suggestion regarding when to use the map() (all map_..() functions) and when to use summarise_at()/mutate_at()? E.g. if we are doing some modification to the column of vectors then we do not need to think map() ? If we have a df /…
CloverCeline
  • 511
  • 3
  • 18
7
votes
3 answers

Purrr filter the nested data based on unnested variable containing character vectors

I have the data similar to df3. To reproduce the data, run the following: vec1 <- c("A", "B") vec2 <- c("A", "B", "C") df1 <- tibble::tribble( ~A, ~B, "X", 4L, "X", 9L, "Y", 5L, "Y", 2L, …
Geet
  • 2,515
  • 2
  • 19
  • 42
7
votes
4 answers

iterate through data frame where each iteration is dependent on the previous item in R efficiently

I have a data frame with two vectors of length 5 and variable: x <- seq(1:5) y <- rep(0,5) df <- data.frame(x, y) z <- 10 I need to loop through the data frame and update y based on a condition related to x using z, and I need to update z at every…
George
  • 1,478
  • 17
  • 28
7
votes
1 answer

map + pmap, cannot find variables

I am trying to collate results from a simulation study using dplyr and purrr. My results are saved as a list of data frames with the results from several different classification algorithms, and I'm trying to use purrr and dplyr to summarize these…
Melissa Key
  • 4,476
  • 12
  • 21
7
votes
1 answer

How to fork/parallelize process in purrr::pmap

I have the following code that does serial processing with purr::pmap library(tidyverse) set.seed(1) params <- tribble( ~mean, ~sd, ~n, 5, 1, 1, 10, 5, 3, -3, 10, 5 ) params %>% pmap(rnorm) #> [[1]] #> [1] 4.373546 #> #>…
neversaint
  • 60,904
  • 137
  • 310
  • 477
7
votes
2 answers

Which is the best way to flatten nested lists derived from a relational database using tidyverse tools?

I have a nested list which have received from a REST call. The response includes a nested set of lists from an underlying relational database. I want to flatten the list to simplify analysis. I have tried to follow the guidelines in the purrr…
rgustavs
  • 97
  • 7
7
votes
2 answers

How to add calculated columns to nested data frames (list columns) using purrr

I would like to perform calculations on a nested data frame (stored as a list-column), and add the calculated variable back to each dataframe using purrr functions. I'll use this result to join to other data, and keeping it compact helps me to…
Matt L.
  • 2,753
  • 13
  • 22