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
8
votes
3 answers

Error using 'segmented' with lm extracted from output of tidyverse 'map' in R

I am using the 'segmented' package to find break points in linear regressions in R library(tidyverse) library(segmented) df <- data.frame(x = c(1:10), y = c(1,1,1,1,1,6:10)) lm_model <- lm(y ~ x, data = df) seg_model <- segmented(obj = lm_model,…
user73522
  • 97
  • 3
8
votes
3 answers

Multiple condition if-else using dplyr, custom function, or purrr

I have a data frame that has similar structure to the following: set.seed(123) df<-data_frame(SectionName = rep(letters[1:2], 50), TimeSpentSeconds = sample(0:360, 100, replace = TRUE), Correct = sample(0:1, 100,…
KyleF
  • 115
  • 1
  • 1
  • 9
8
votes
3 answers

Add multiple output variables using purrr and a predefined function

Take this simple dataset and function (representative of more complex problems): x <- data.frame(a = 1:3, b = 2:4) mult <- function(a,b,n) (a + b) * n Using base R's Map I could do this to add 2 new columns in a vectorised fashion: ns <-…
thelatemail
  • 91,185
  • 12
  • 128
  • 188
8
votes
2 answers

apply/map a different function per row in a data frame with varying parameters

I have a simple problem for you purrr-experts out there that has eluded my best googling efforts for some time. First, let's take a look at the nested-list data structure I'm trying to work with. Load packages #R version 3.4.1 library(purrr) #…
vergilcw
  • 2,093
  • 4
  • 16
  • 20
8
votes
1 answer

Error using dplyr::count() within purrr::map()

In this example I want to apply the count() function to every character variable in a dataset. library(dplyr) library(purrr) nycflights13::flights %>% select_if(is.character) %>% map(., count) But I receive the error message: Error in…
Joe
  • 3,217
  • 3
  • 21
  • 37
8
votes
2 answers

Dummy code categorical / ordinal variables in the tidyverse r

Let's say I have a tibble. library(tidyverse) tib <- as.tibble(list(record = c(1:10), gender = as.factor(sample(c("M", "F"), 10, replace = TRUE)), like_product = as.factor(sample(1:5, 10, replace =…
Jacob Nelson
  • 443
  • 1
  • 6
  • 16
8
votes
2 answers

Applying function (ks.test) between two data frames column-wise in R

My simple question is: How do you do a ks.test between two data frames column by column? Eg. We have two data frames: D1 <- data.frame(D$Ag, D$Al, D$As, D$Ba, D$Be, D$Ca, D$Cd, D$Co, D$Cu, D$Cr) D2 <- data.frame(S$Ag, S$Al, S$As, S$Ba, S$Be, S$Ca,…
Ib Nemer
  • 85
  • 1
  • 6
8
votes
2 answers

map a vector of characters to lm formula in r

I'm trying to make a list of lm object using purrr::map. use mtcars as an example: vars <- c('hp', 'wt', 'disp') map(vars, ~lm(mpg~.x, data=mtcars)) error: Error in model.frame.default(formula = mpg ~ .x, data = mtcars, drop.unused.levels =…
zesla
  • 11,155
  • 16
  • 82
  • 147
8
votes
2 answers

How do pipes work with purrr map() function and the "." (dot) symbol

When using both pipes and the map() function from purrr, I am confused about how data and variables are passed along. For instance, this code works as I expect: library(tidyverse) cars %>% select_if(is.numeric) %>% map(~hist(.)) Yet, when I…
D.Hadley
  • 1,179
  • 13
  • 13
8
votes
3 answers

Why is split inefficient on large data frames with many groups?

df %>% split(.$x) becomes slow for large number of unique values of x. If we instead split the data frame manually into smaller subsets and then perform split on each subset we reduce the time by at least an order of…
Rickard
  • 3,600
  • 2
  • 19
  • 22
8
votes
3 answers

Purrr-Fection: In Search of An Elegant Solution to Conditional Data Frame Operations Leveraging Purrr

The Background I have an issue for which a number of solution pathways are possible, but I am convinced there is an as-yet-undiscovered elegant solution leveraging purrr. The Example Code I have a large data frame as follows, for which I have…
amormachine
  • 405
  • 4
  • 10
7
votes
5 answers

How to keep first level list elements in a multi level nested list of lists

I have a list with many levels. I want to only keep elements at the 1st level. Example list: my_list <- list( a.1 = "some text", b.1 = NA, c.1 = integer(0), d.1 = "some text", e.1 = list(a.2 = "some text", b.2 = "a2"), …
nycrefugee
  • 1,629
  • 1
  • 10
  • 23
7
votes
4 answers

Return all combinations of two column types that sum to >=0 and return corresponding summary metadata for which columns in R [R]

I have data like this: example_df <- data.frame( col1type1 =c(110:106), col2type2 = c(-108:-104), col3type1 = c(-109:-105), col4type2 =c(110:106), col5type1 =c(107:103), col6type2 = c(-110:-106), col7type1 =c(109:113), col8type2 =…
Neal Barsch
  • 2,810
  • 2
  • 13
  • 39
7
votes
4 answers

Use a function name that's a string in map loop?

Some code: mymtcars <- mtcars %>% head %>% rownames_to_column('model') %>% group_by(vs) %>% nest mymtcars vs data 1 0 2 1 I can fit a linear model on this list…
Doug Fir
  • 19,971
  • 47
  • 169
  • 299
7
votes
2 answers

Difference map() vs. map_dfr() in R

While playing around with the purrr package of the Tidyverse in R, I saw that the map() function returns a list. library(tidyverse) set.seed(123) map(1:5, ~rnorm(3)) #> [[1]] #> [1] -0.5604756 -0.2301775 1.5587083 #> #> [[2]] #> [1] 0.07050839…
user213544
  • 2,046
  • 3
  • 22
  • 52