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

Mutate multiple / consecutive columns (with dplyr or base R)

I'm trying to create "waves" of variables that represent repeated measures. Specifically, I'm trying to create consecutive variables that represent the mean values for variables 1 - 10, 11 - 20 ... 91-100. Note that the "..." symbolizes the…
Joshua Rosenberg
  • 4,014
  • 9
  • 34
  • 73
9
votes
6 answers

Converting grouped tibble to named list

I feel like there is probably a better way to do this in tidyverse than a for-loop. Start with a standard tibble/dataframe, and make a list where the name of the list elements are the unique values of one column (group_by?) and the list elements are…
Jeff Parker
  • 1,809
  • 1
  • 18
  • 28
9
votes
3 answers

Mutate across multiple columns to create new variable sets

I have a panel dataset at the country and year level, and I'd like to create a two new variables based on existing ones. year country var1 var2 var3 var…
PierreRoubaix
  • 167
  • 1
  • 1
  • 7
9
votes
2 answers

Apply function to a row in a data.frame using dplyr

In base R I would do the following: d <- data.frame(a = 1:4, b = 4:1, c = 2:5) apply(d, 1, which.max) With dplyr I could do the following: library(dplyr) d %>% mutate(u = purrr::pmap_int(list(a, b, c), function(...) which.max(c(...)))) If there’s…
thothal
  • 16,690
  • 3
  • 36
  • 71
9
votes
2 answers

Giving the list returned by purrr::map names

Is there a way to automatically give names to the returned list given by purrr:map? For example, I run code like this very often. fn <- function(x) { paste0(x, "_") } l <- map(LETTERS, fn) names(l) <- LETTERS I'd like for the vector that is being…
user1775655
  • 317
  • 2
  • 8
9
votes
5 answers

Summing Multiple Groups of Columns

I have a situation where my data frame contains the results of image analysis where the columns are the proportion of a particular class present in the image, such that an example dataframe class_df would look like: id A B C D E F …
Syzorr
  • 587
  • 1
  • 5
  • 17
9
votes
3 answers

using purrr to map dplyr::select

I have a data frame with a bunch of nested data-frames within it, and I'd like to apply dplyr::select to each of those nested data frames. Here's an example library(tidyverse) mtcars %>% group_by(cyl) %>% nest %>% mutate(data2 = ~map(data,…
ohnoplus
  • 1,205
  • 1
  • 17
  • 29
9
votes
3 answers

loess regression on each group with dplyr::group_by()

Alright, I'm waving my white flag. I'm trying to compute a loess regression on my dataset. I want loess to compute a different set of points that plots as a smooth line for each group. The problem is that the loess calculation is escaping the…
Alex Nesta
  • 393
  • 2
  • 13
9
votes
1 answer

Using pipes within map() in R

I'm getting unexpected results when attempting to pipe inside map() map(ls(), ~ . %>% get %>% dim) returns the following message: Functional sequence with the following components: 1. get(.) 2. dim(.) Use 'functions' to extract the individual…
vuzun
  • 942
  • 2
  • 8
  • 15
9
votes
2 answers

Combined fuzzy and exact matching

I have two tables containing addresses (street, city, zipcode and two fields containing concatenated values of these), I would like to do fuzzy matching on Zipcode, but only for those cases which have exact same StrCity value. I have started with…
PrzeM
  • 211
  • 3
  • 15
9
votes
2 answers

using purrr to affect single columns of each dataframe in a list

still getting used to purrr and I have one of those questions that I think should be easy, but I don't know how to do it. All I want to do is convert the datetimes in the below, to dates with as.Date(). it's a list of dataframes. Been playing around…
jsg51483
  • 193
  • 1
  • 10
9
votes
2 answers

What is the **tidyverse** method for splitting a df by multiple columns?

I would like to split a dataframe by multiple columns so that I can see the summary() output for each subset of the data. Here's a way to do that using split() from base: library(tidyverse) #> Loading tidyverse: ggplot2 #> Loading tidyverse:…
Tiernan
  • 828
  • 8
  • 20
9
votes
2 answers

Extract elements from nested list only using functions from purrr package

How do I extract elements from a nested list only using the purrr package? In this case I want to get a vector of intercepts after splitting a data.frame. I have accomplished what I need using lapply(), but I would like to use only functions purrr…
hackR
  • 1,459
  • 17
  • 26
8
votes
6 answers

Coalesce pairs of variables within a dataframe based on a regular expression

I want to use dplyr::coalesce to find the first non-missing value between pairs of variables in a dataframe containing multiple pairs of variable. The goal is to create a new dataframe with now only one copy for each pair of variable (a coalesce…
8
votes
6 answers

Get column names and dataframe name from a list of dataframes into a single dataframe

I am looking for a way to make column names and dataframe names from a list of dataframes into a single dataframe. They have unequal length of columns. What's the best way to do this? dlist <- list(mtcars[1:2], mtcars[1:3], mtcars[1:4]) names(dlist)…
codedancer
  • 1,504
  • 9
  • 20