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

Applying `unnest_wider()` recursively to all columns, until no more nested list columns remain?

A follow-up to this question: is it possible to apply unnest_wider recursively, until no more list columns are left? I'm working with a deeply nested data set, where one list column has a single level (one unnest_wider call does the trick); but…
Khashir
  • 341
  • 3
  • 20
6
votes
3 answers

predict values from GAM for grouped dataframe in R

I have a dataset of mean annual temperature values at different latitudes in different years. I want to use this to predict the latitude at which a given temperature could be found in a given year; i.e., "in 1980, at what latitude would the mean…
AFH
  • 665
  • 1
  • 9
  • 28
6
votes
1 answer

Use function arguments in purrr::possibly otherwise

Is there a way to use the arguments passed to the original function in the otherwise argument of purrr::possibly()? For example (this is a dumb toy example--the real code involves web scraping behind a password-protected…
crazybilly
  • 2,992
  • 1
  • 16
  • 42
6
votes
3 answers

Convert list column to column of strings

My data consist of an identifier (srdr_id), and a list column. dat <- structure(list(srdr_id = c("174136", "174258", "174684"), outcomes = list( structure(list(outcome_s = c("use_alcohol", "use_cannabis", "use_cocaine")), class =…
user25494
  • 1,289
  • 14
  • 27
6
votes
2 answers

purrr opposite of pluck (throw away element) + preserving list structure in pluck

Suppose there is a list as follows: x <- list(a = list(a1 = 1, a2 = 2), b = list(a2 = 3, a1 = 4)) The positions/names are mixed in the sublists, and to pluck out the a1s from the list, I would do the following in purrr. x %>% map(purrr::pluck,…
Kim
  • 4,080
  • 2
  • 30
  • 51
6
votes
5 answers

Tidy method to split multiple columns using tidyr::separate

I have a data frame like so: df <- structure(list(A = c("3 of 5", "1 of 2", "1 of 3", "1 of 3", "3 of 4", "2 of 7"), B = c("2 of 2", "2 of 4", "0 of 1", "0 of 0", "0 of 0", "0 of 0"), C = c("10 of 21", "3 of 14", "11 of 34", "10 of 35", "16 of…
Shinobi_Atobe
  • 1,793
  • 1
  • 18
  • 35
6
votes
1 answer

R - Using purrr to replace NULL elements with NA in a list of lists

I am trying to replace the NULL elements of the list below with NAs inside a map() before using rbindlist on the cleaned list: m = list(min = list(id = "min", val = NULL), max = list(id = "max", val = 7), split = list(id = "split", val =…
user51462
  • 1,658
  • 2
  • 13
  • 41
6
votes
2 answers

Get title for plots when using purrr and ggplot with group_by and nest()

I have the following example: df <- mtcars plot <- df %>% mutate(carb=as.character(carb)) %>% group_by(carb) %>% nest() %>% mutate(plot=map(data, function(.x){ .x %>% ggplot() + geom_bar(aes(mpg)) })) print(plot) # A…
xhr489
  • 1,957
  • 13
  • 39
6
votes
3 answers

How to bind two lists with same structure?

Introduction I have two nested lists with the same structure that I'd like to combine (in the c() sense). There might already exist a concept for what I mean by same structure in graph theory, or in computer science, for this relationship but I am…
Ramiro Magno
  • 3,085
  • 15
  • 30
6
votes
2 answers

select non-missing variables in a purrr loop

Consider this example mydata <- data_frame(ind_1 = c(NA,NA,3,4), ind_2 = c(2,3,4,5), ind_3 = c(5,6,NA,NA), y = c(28,34,25,12), group = c('a','a','b','b')) >…
ℕʘʘḆḽḘ
  • 18,566
  • 34
  • 128
  • 235
6
votes
2 answers

How to use map() with possibly()

I am using map() to get post data from Facebook using the following code: posts_data <- map(posts$query_id, getPost, token = fb_oauth, n = 1000) However, some of the query_id observations are incorrect, or are shared events, which the API cannot…
mundos
  • 459
  • 6
  • 14
6
votes
2 answers

confusing behavior of purrr::pmap with rlang; "to quote" or not to quote argument that is the Q

I have a custom function where I am reading entered variables from a dataframe using rlang. This function works just fine irrespective of whether the arguments entered are quoted or unquoted. But, strangely enough, when this function is used with…
Indrajeet Patil
  • 4,673
  • 2
  • 20
  • 51
6
votes
3 answers

How can I use accumulate like reduce2 function in purrr?

I would like to use the accumulate function with two input vectors and the reduce2 function. The documentation for accumulate implies that two input vectors can be given and that accumulate can work with reduce2. However, I am having trouble. Here…
Steve
  • 135
  • 1
  • 6
6
votes
1 answer

write from nested dataframe with on-the-fly filename using purrr::walk

I'm applying a function to a nested dataframe using purrr::map to get a new dataframe list column. Now I want to write each of these new dataframes to file using column values from the same row as part of the filename. I'm stuck on how to pull the…
mark
  • 537
  • 6
  • 25
6
votes
1 answer

Supplying multiple groups of variables to a function for dplyr arguments in the body

Here is the data: library(tidyverse) data <- tibble::tribble( ~var1, ~var2, ~var3, ~var4, ~var5, "a", "d", "g", "hello", 1L, "a", "d", "h", "hello", 2L, "b", "e", "h", "k", 4L, "b", "e", "h", …
Geet
  • 2,515
  • 2
  • 19
  • 42