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
1
vote
1 answer

Pass a list of lists of unquoted character parameters to an apply/map/pmap call

I have created a function to do a certain type of analysis: library(tidyverse) library(mediation) causal_med_so <- function(predictor, mediator, outcome, data, ...){ if(!missing(...)) { data <- {{data}} %>% …
Raoul Duke
  • 435
  • 3
  • 13
1
vote
3 answers

Splitting by two categories in R

n <- 3 strata <- rep(1:4, each=n) y <- rnorm(n =12) x <- 1:12 category <- rep(c("A", "B", "C"), times = 4) df <- cbind.data.frame(y, x, strata, category) I want to first split my data into a list by "strata", and then I want to again split all the…
Muhammad Kamil
  • 635
  • 2
  • 15
1
vote
1 answer

how to use dynamic variable in purrr map within dplyr

I have the following data which looks like that: tibble( name = paste0("segment",1), data = list(tibble(segment1 = 1:5, check = 99)) ) # A tibble: 1 x 2 name data 1 segment1 I…
Christian
  • 401
  • 1
  • 5
  • 14
1
vote
2 answers

Native pipe with purrr::map_dfr()

I'd like to use the new native pipe,|>, with purrr::map_dfr(). (To make it reproducible, I'm passing the datasets as strings instead of paths, but that shouldn't make a difference.) csvs <- c( "csv_a" = "a,b,c\n1,2,3\n4,5,6", "csv_b" =…
wibeasley
  • 5,000
  • 3
  • 34
  • 62
1
vote
0 answers

Using past prediction as dependent variable with slider

I'm stuck on a problem that I can't solve no matter how hard I try. Imagine that you have 3 variables as described below. The y that you want to predict has 5 observations less than a or b. What I want is: run an OLS with the last 10 obs and predict…
1
vote
4 answers

How best to split strings in tibble and keep only the item that matches a desired string in dplyr/R?

I'm processing someone else's data and they have a column of various genes and protein changes within those genes. I only care about one gene and wish to extract the protein changes (the numbers in this example). I've arrived at a solution that's…
GenesRus
  • 1,057
  • 6
  • 16
1
vote
1 answer

How do I download and save multiple files from google drive to a particular folder on my laptop using R and googledrive package

I'm following the instructions in this post to download 2 csv files from my google drive to my computer. Here is the code provided - library(googledrive) library(purrr) ## store the URL you have folder_url <-…
The Rookie
  • 877
  • 8
  • 15
1
vote
3 answers

How to estimate the mean of the 10% upper and lower values over multiple categories with dplyr?

Suppose you have this data.frame in R set.seed(15) df <- data.frame(cat = rep(c("a", "b"), each = 50), x = c(runif(50, 0, 1), runif(50, 1, 2))) I want to estimate the mean of the 10% upper and lower values in each category. I…
Maikol Solís
  • 71
  • 1
  • 5
1
vote
1 answer

R pass function to accumulate

I want to use this accumulate example changing the fixed "growth" for a variable in the data farme. Original example:…
AngeG
  • 91
  • 10
1
vote
1 answer

How to use map and ggplot to add custom titles to each chart?

I have a dataset that I want to make simple bar charts for each variable that has their appropriate title by using map() from purrr. My dataset has 4 variables, and I have a vector with 4 titles. How do I pass these titles through so R outputs a…
J.Sabree
  • 2,280
  • 19
  • 48
1
vote
1 answer

How could I better refactor the following code in R using purrr

My function parse_columns takes four arguments: A data.frame \ tibble: df A character vector representing a subset of the columns in the tibble: vars A pattern: pattern The name of the new output column to be created in the same tibble:…
Three14
  • 43
  • 5
1
vote
1 answer

Feed a vector of values into power function

I want to estimate the minimum detectable effect size for various levels of N and SD. Power is always fixed at 0.8 and alpha is always fixed at 0.05. This is my code so far: library(pwr) power_fcn <- function(.x){ power.t.test(n = .x, d =…
C.Robin
  • 1,085
  • 1
  • 10
  • 23
1
vote
1 answer

R writing operations into a function and iterating

I have this input (sample): input <- tibble( minimum_term = c("None", "6 Months", "12 Months"), maximum_term = c("None", "18 Months", "24 Months"), other_cols ) and I would like to get to this output: desired_output <- tibbe( minimum_term =…
Three14
  • 43
  • 5
1
vote
2 answers

rbind multiple lists of dataframes into a list

I am looking for a more elegant or efficient solution of appending multiple lists of data frames into a single list. For simplicity, let's assume we deal with the following case: my_func <- function(i) { # function to generate a list with two data…
AlexB
  • 3,061
  • 2
  • 17
  • 19
1
vote
1 answer

recode data function other data

I want to recodate my data, function of decisions rules An example of rules : Data with more than 3 variables years First rule :we corrected data if only one error : y ≤ y+2 and y+1 < y then y+1 = y After the previous correction, corrected with…
Nic
  • 143
  • 5