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

Simulating data with the purrr::map-family: truncated normal distribution for each row consumes the RAM

I try to assess the combined uncertainties related to different input parameters using Markov Chain Monte Carlo method in R. In other words, using the uncertainty parameters reported in input data documentation, I try to generate distributions for…
1
vote
1 answer

Control the order of pmap output in R

I have a lot of statistical models to run and I am trying to use pmap to loop over the variable names needed for each model. I want to return the model summary along with extra information about each model. I'm able to run the models and return…
1
vote
2 answers

R: web-scraping returns wrong prices for products

May someone look at why my code returns wrong prices for the products? For example, let's look at this tv: TELEVISOR HISENSE LED ULTRA HD 4K 55" SMART TV 55A6GSV -Precio antes (NORMAL) in web page: S/ 2,299 -Precio antes (NORMAL) in my results: S/…
Omar Gonzales
  • 3,806
  • 10
  • 56
  • 120
1
vote
1 answer

purrr::flatten a list but maintain the hierarchy name

Suppose I have a list of named vectors: test_list <- list("grp1" = c(subgrp1 = "A", subgrp2 = "B"), "grp2" = c(subgrp1 = "A", subgrp2 = "B"), "grp3" = c(subgrp1 = "A")) I want to change this into want_list <-…
aiorr
  • 547
  • 4
  • 11
1
vote
1 answer

Add a row to each dataframe in a list with the column median using map_dfr

I have this example list that contains 3 dataframes: library(tidyverse) list_df <- iris %>% group_by(Species) %>% slice(1:3) %>% ungroup() %>% group_split(Species) I want to add a new row at the end of each dataframe that shows…
TarJae
  • 72,363
  • 6
  • 19
  • 66
1
vote
3 answers

Subset dataframes in a list based on a single value of a corresponding vector

I am trying to conditionally subset data.frames in a list of data.frames based on value in a vector. Basically, whenever a > 0 I would like to subset the corresponding list element to have that many randomly-sampled rows. # a list l <- list(…
xilliam
  • 2,074
  • 2
  • 15
  • 27
1
vote
1 answer

How to apply a function on multiple files from same sample and combine them?

I have many Sample files in a directory Data. There are around 80 samples. Each sample has 3 output files with different formats having sample name as a prefix. The setup looks like below. I'm showing only a few sample files here. Data |___…
maven
  • 15
  • 5
1
vote
3 answers

Add column in dataframe based on 3 columns from another dataframe using R

I have 2 dataframes which are as follows: Dataframe 1: traffic_df which is hourly data. Date_Time Traffic 2020-03-09 06:00:00 10 2020-03-09 07:00:00 20 2020-03-10 07:00:00 20 2020-03-24 08:00:00 15 Dataframe 2:…
csharpvsto
  • 89
  • 1
  • 8
1
vote
3 answers

Modifying/Correcting Incorrect Year in R

I have a dataframe which has some erroneous rows where the year is incorrect. Is there a way to elegantly change a specific year that I know is incorrect ("2017") to the correct year ("2019"). Example…
csharpvsto
  • 89
  • 1
  • 8
1
vote
0 answers

reduce and do.call work differently for 1-element list

I use map/reduce on a vector that sometimes has only one element. It returns a numeric vector instead of a matrix. do.call works as expected test1 <- 1 map_l1 <- map(test1, ~ .x*c(1,2,3)) r1 <- reduce(map_l1, rbind) r1 d1 <- do.call("rbind",…
dorit
  • 41
  • 5
1
vote
1 answer

Add a purrr:slowly or sys.sleep in a map

I created a function to scrape the fbref.com website. Sometimes in this website or in others that I'm trying to scrape I receive a timeout error. I read about it and it is suggested to include a sys.sleep between the requisitions or a purrr:slowly.…
1
vote
1 answer

Keeping the max within a group constant within a group using base::cumsum

Use the data below to make the cumsum_a column look like the should column. Data to start with: > demo th seq group 1 20.1 1 10 2 24.1 2 10 3 26.1 3 10 4 1.1 1 20 5 2.1 2 20 6 4.1 3 20 The "should" column below…
CrunchyTopping
  • 803
  • 7
  • 17
1
vote
1 answer

split or group_split dataframe by multiple columns at once

I'm sure this has been asked but can't find what I'm looking for. What is the best way to split (or group_split) over multiple columns at the same time but separately. Given: library(tidyverse) #example data set.seed(1) df <- data.frame( group =…
user63230
  • 4,095
  • 21
  • 43
1
vote
1 answer

In R, sample enough rows in a dataframe that no column is empty

I have a large dataframe df in R with many columns, many NAs, but no column is entirely NA. I am interested in a specific list col_list of those columns. I want a sample of the dataframe so that each column is represented at least once. My thought…
Rob Creel
  • 563
  • 2
  • 13
1
vote
1 answer

problem using a dynamic trend or seasonal parameter with ETS with fable and purrr

I have a tsibble as shown below. test.data <- structure(list(RSLITM = c("004", "004", "004", "004", "004", "004", "004", "004", "004", "004", "004", "004", "004", "004", "004", "004", "004", "004", "004", "004", "004", "004", "004", "004", "004",…
LauraDR
  • 86
  • 9