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

Using purrr::map to iterate linear model over columns in data frame

I am trying to do an exercise to become more familiar with how to use the map function in purrr. I am creating some random data (10 columns of 10 datapoints) and then I wanted to use map to perform a series of regressions (i.e. lm(y ~ x, data = ))…
LucaS
  • 887
  • 1
  • 9
  • 22
7
votes
3 answers

Extract model summaries and store them as a new column

I'm new to the purrr paradigm and am struggling with it. Following a few sources I have managed to get so far as to nest a data frame, run a linear model on the nested data, extract some coefficients from each lm, and generate a summary for each…
niklz
  • 95
  • 7
7
votes
2 answers

modelr: Fitting multiple models with resampled data

In the tidy model of data science (TM) implemented in modelr, resampled data are organized using list-columns: library(modelr) library(tidyverse) # create the k-folds df_heights_resampled = heights %>% crossv_kfold(k = 10, id = "Resample…
tchakravarty
  • 10,736
  • 12
  • 72
  • 116
7
votes
1 answer

Double nesting in the tidyverse

Using the examples from Wickhams introduction to purrr in R for data science, I am trying to create a double nested list. library(gapminder) library(purrr) library(tidyr) gapminder nest_data <- gapminder %>% group_by(continent) %>% nest(.key =…
Misha
  • 3,114
  • 8
  • 39
  • 60
7
votes
1 answer

Error: missing values and NaN's not allowed if 'na.rm' is FALSE

Trying out multiple models chapter of #r4ds and ran into an error message at the end: Error: missing values and NaN's not allowed if 'na.rm' is FALSE In addition: Warning message: In ns(as.numeric(Month), 4) : NAs introduced by…
user12081
  • 147
  • 1
  • 3
  • 9
7
votes
2 answers

How to get list name and slice name with pipe and purrr

I wonder how to get the list name or group name as a flag when using pipe operation with purrr. for example: I want to use a dynameic parameter of each list name pass to the ggsave…
earclimate
  • 375
  • 1
  • 14
6
votes
5 answers

List of vectors to list of lists

I have a list structured this way: x <- list(id = c("a", "b"), value = c(1,2), othervalue = c(3,4) ) I need to transform the list to this structure like this: y <- list(a = list(value = 1, othervalue = 3), b =…
Felix M
  • 122
  • 5
6
votes
3 answers

Join a dataframe with multiple dataframes stored in a list with different column

I have a dataframe (df1) and a list of dataframes (test) like below; I want to join df1 with each of the datafraems in test and populate a new column (X), while keeping all the other records intact. read.table(text = "Fruits A B C D …
M--
  • 25,431
  • 8
  • 61
  • 93
6
votes
5 answers

do() superseded! Alternative is to use across(), nest_by(), and summarise, how?

I'm doing something quite simple. Given a dataframe of start dates and end dates for specific periods I want to expand/create a full sequence for each period binned by week (with the factor for each row), then output this in a single large…
Dasr
  • 777
  • 6
  • 16
6
votes
2 answers

Using accumulate function with second to last value as .init argument

I have recently come across an interesting question of calculating a vector values using its penultimate value as .init argument plus an additional vector's current value. Here is the sample data set: set.seed(13) dt <- data.frame(id =…
Anoushiravan R
  • 21,622
  • 3
  • 18
  • 41
6
votes
2 answers

How to use ifelse inside map function in R

I am having problems with this ifelse sentence inside map function: df<-list(mtcars,mtcars) All I want to do is to organize each dataframe of this list this way:slice(x,c(n(),2:(n()-1),1)) map(df, ~ slice(.x,c(n(),2:(n()-1),1))) # it works…
Laura
  • 675
  • 10
  • 32
6
votes
1 answer

How to replace NULL values over arbitrarily nested list in R?

I have an arbitrarily nested list (no rules). I need to replace NULL values with NA and the structure of the list needs to stay in tact. Minimal working example: myList <- list("elem1" = "first", "elem2" = list("elem2.1" = "second1", …
Brigadeiro
  • 2,649
  • 13
  • 30
6
votes
2 answers

Using map and pluck to get values from nested list

I have the following nasty, nested list Edit: updated to include value_I_dont_want mylist <- list( list( nested_1 = list( nested_2 = list( list( value_I_want = "a", value_I_dont_want = "f"), list( value_I_want = "b",…
MayaGans
  • 1,815
  • 9
  • 30
6
votes
2 answers

Iterating over multiple regression models and data subsets in R

I am trying to learn how to automate running 3 or more regression models over subsets of a dataset using the purrr and broom packages in R. I am doing this with the nest %>% mutate(map()) %>% unnest() flow in mind. I am able to replicate examples…
user11151932
  • 223
  • 1
  • 5
6
votes
4 answers

Split a list into separate data frame in R

So I have a list with me as below, what I want is to split them into three separate dataframes (with names as Banana/Strawberry & apple) as shown in expected output. I have already seen this (Splitting List into dataframe R) but its exact opposite…
Vaibhav Singh
  • 1,159
  • 1
  • 10
  • 25