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
240
votes
5 answers

Why use purrr::map instead of lapply?

Is there any reason why I should use map(, function(x) ) instead of lapply(, function(x) ) the output should be the same and the benchmarks I made seem to show that lapply is slightly faster…
Tim
  • 7,075
  • 6
  • 29
  • 58
58
votes
6 answers

Row-wise iteration like apply with purrr

How do I achieve row-wise iteration using purrr::map? Here's how I'd do it with a standard row-wise apply. df <- data.frame(a = 1:10, b = 11:20, c = 21:30) lst_result <- apply(df, 1, function(x){ var1 <- (x[['a']] + x[['b']]) …
matsuo_basho
  • 2,833
  • 8
  • 26
  • 47
51
votes
9 answers

Adding column if it does not exist

I have a bunch of data frames with different variables. I want to read them into R and add columns to those that are short of a few variables so that they all have a common set of standard variables, even if some are unobserved. In other words... Is…
guyabel
  • 8,014
  • 6
  • 57
  • 86
44
votes
4 answers

purrr map equivalent of nested for loop

What is the purrr::map equivalent of: for (i in 1:4) { for (j in 1:6) { print(paste(i, j, sep = "-")) } } OR lapply(1:4, function(i) lapply(1:6, function(j) print(paste(i, j, sep = "-")))) Conceptually, what I'm not getting is how…
merov
  • 443
  • 1
  • 5
  • 4
44
votes
3 answers

Use input of purrr's map function to create a named list as output in R

I am using the map function of the purrr package in R which gives as output a list. Now I would like the output to be a named list based on the input. An example is given below. input <- c("a", "b", "c") output <- purrr::map(input, function(x)…
Michael
  • 1,281
  • 1
  • 17
  • 32
33
votes
8 answers

Tidyverse approach to binding unnamed list of unnamed vectors by row - do.call(rbind,x) equivalent

I often find questions where people have somehow ended up with an unnamed list of unnamed character vectors and they want to bind them row-wise into a data.frame. Here is an example: library(magrittr) data <-…
Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
28
votes
3 answers

R purrr:::pmap: how to refer to input arguments by name?

I am using R purrr:::pmap with three inputs. It is not clear how I can refer explicitly to these inputs in the formula call? When using map2, the formula call goes as ~ .x + .y. But how to do when using pmap? Reproducing Hadley's example from…
Matifou
  • 7,968
  • 3
  • 47
  • 52
27
votes
5 answers

tidyverse - prefered way to turn a named vector into a data.frame/tibble

Using the tidyverse a lot i often face the challenge of turning named vectors into a data.frame/tibble with the columns being the names of the vector. What is the prefered/tidyversey way of doing this? EDIT: This is related to: this and this…
Rentrop
  • 20,979
  • 10
  • 72
  • 100
23
votes
1 answer

What is meaning of first tilde in purrr::map

I was looking at this example that uses map. Here it is: mtcars %>% split(.$cyl) %>% # from base R map(~ lm(mpg ~ wt, data = .)) What is the meaning of the first tilde in map(~ lm...? That is, how does R interpret the first tilde? (I understand…
CPak
  • 13,260
  • 3
  • 30
  • 48
22
votes
10 answers

How to use map from purrr with dplyr::mutate to create multiple new columns based on column pairs

I have to following issue using R. In short I want to create multiple new columns in a data frame based on calculations of different column pairs in the data frame. The data looks as follows: df <- data.frame(a1 = c(1:5), b1 =…
user30276
  • 323
  • 1
  • 2
  • 4
21
votes
1 answer

Parse Error: "Trailing Garbage" while trying to parse JSON column in data frame

I have a log file that look like this. This is a text document that looks like: Id,Date,Level,Message 35054,2016-06-17 19:29:43 +0000,INFO,"{ ""id"": -2, ""ipAddress"": ""100.100.100.100"", ""howYouHearAboutUs"": null, …
emehex
  • 9,874
  • 10
  • 54
  • 100
18
votes
4 answers

Does a multi-value purrr::pluck exist?

Seems like a basic question and perhaps I'm just missing something obvious ... but is there any way to pluck a sublist (with purrr)? More specifically, here's an initial list: l <- list(a = "foo", b = "bar", c = "baz") And I want to return a new…
mmuurr
  • 1,310
  • 1
  • 11
  • 21
17
votes
3 answers

Pass function arguments by column position to mutate_at

I'm trying to tighten up a %>% piped workflow where I need to apply the same function to several columns but with one argument changed each time. I feel like purrr's map or invoke functions should help, but I can't wrap my head around it. My data…
camille
  • 16,432
  • 18
  • 38
  • 60
16
votes
7 answers

tidyverse: binding list elements of same dimension

Using reduce(bind_cols), the list elements of same dimension may be combined. However, I would like to know how to combine only same dimension (may be specified dimesion in some way) elements from a list which may have elements of different…
MYaseen208
  • 22,666
  • 37
  • 165
  • 309
16
votes
1 answer

How to use purrr::pmap to plot multiple ggplot in nested.data.frame

I have some questions about purrr::pmap to make multiple plots of ggplot in nested.data.frame. I can run below code without problem by using purrr::map2 and I can make multiplots(2 plots) in nested.data.frame. As a example, I used the iris…
Lc_decg
  • 187
  • 1
  • 10
1
2 3
99 100