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
2 answers

Populate a dataframe column using a function that applies an equation to a reference table

Suppose you want to calculate how far away a patient's BMI is away from the population median using a Z-score. This is calculated using the patient's BMI plus three age- and sex-dependent variables. These three variables are looked up in a…
Casey
  • 125
  • 6
1
vote
2 answers

imap() not working as I expected, what am I missing?

So here, i get errors with imap, when using the index (".y") to go through a list. below I have made it work with map2, but this is confusing, because the way I made the map2() function is exacly the same way I would've thought that imap would do…
emilBeBri
  • 625
  • 13
  • 18
1
vote
3 answers

Map over list of dataframes and apply custom mutate-function (purrr, dplyr)

So I have this list: list(`0` = structure(list(fn = 0L, fp = 34L, tn = 0L, tp = 34L), row.names = c(NA, -1L), class = c("tbl_df", "tbl", "data.frame")), `0.1` = structure(list( fn = 1L, fp = 26L, tn = 8L, tp = 33L), row.names = c(NA, -1L),…
Lenn
  • 1,283
  • 7
  • 20
1
vote
1 answer

map over every combination of lat/lon values and store in a matrix the distance

I am trying to compute some distances for each combination of lat/lon values I have. The first data frame looks like: lon lat NOMVIAL 1 -99.1 19.5 Tepozanes 2 -99.0 19.3…
user113156
  • 6,761
  • 5
  • 35
  • 81
1
vote
1 answer

Extract list element from column of dataframe using R

I have a dataframe where the third column is a list of lists. I wish to add a column to the existing dataframe that consists only of the element of the list where key = wb_id and the value string is what I want to be in the new column. Previously, I…
ixodid
  • 2,180
  • 1
  • 19
  • 46
1
vote
2 answers

how to bind the col names as first row to each data.frame in a list

I have a list of list, which each sublist also have multi dfs. I would like to bind names(df) to df. How can I do that? Data: q1<-list(Demographics = list(`101-01-101` = structure(list(SubjectID = "101-01-101", BRTHDTC = "1953-07-07", SEX =…
Stataq
  • 2,237
  • 6
  • 14
1
vote
2 answers

how to define a df in a sublist using imap

I have a list of list, and each sublist also have multiple df. Now I would like to know the number of cols in each df in each sublist using imap. How can I get point to the df correctly. Sample list can be built using: lst1<-list(`101-01-101` =…
Stataq
  • 2,237
  • 6
  • 14
1
vote
0 answers

Filtering by position in purrr::cross3

I have a list that is produced by the combinations of the elements of three other lists (in this example I will use vectors) through the cross3() command of purrr. a<- cross3(5:1,1:25,1:5) I only need the combinations that will be on specific…
User800701
  • 159
  • 10
1
vote
2 answers

Counting the number of row matches across a dynamic number of columns

Goal: I have a table of records (for example people) with a dynamic number of variables (e.g., email, phone, birthday,..., others). I want to compare each row with every other row and sum the number of variables that match. # Input my_data <-…
Jeff Parker
  • 1,809
  • 1
  • 18
  • 28
1
vote
1 answer

Get predicted value out of a betaRegression model when using `nest()`

I'm running some regression analyses using a nested file. The code that I have is similar to this: library(tidyverse) library(purrr) library(betareg) # 1. Create dataframe ---- dt <- data.frame(Marker = as.factor(paste0('m', rep(seq(1,10), 10))), …
godines
  • 349
  • 4
  • 9
1
vote
3 answers

How best to create a new column for each two-column comparison using purrr?

Say I have the following dataframe: ABC1_old <- c(1, 5, 3, 4, 3, NA, NA, NA, NA, NA) ABC2_old <- c(4, 2, 1, 1, 5, NA, NA, NA, NA, NA) ABC1_adj <- c(NA, NA, NA, NA, NA, 5, 5, 1, 2, 4) ABC2_adj <- c(NA, NA, NA, NA, NA, 3, 2, 1, 4, 2) df <-…
Natasha R.
  • 521
  • 1
  • 5
  • 11
1
vote
1 answer

Clarity about map_chr() function in dplyr and mutate?

I am sorry, the functional programming "loops" got me a bit in of head scratching about purrr. I know that to use own, non-vectorised functions one can use map_chr() and I used it together with mutate to produce 2 new columns. But at one point I did…
r0berts
  • 842
  • 1
  • 13
  • 27
1
vote
1 answer

R: Passing different-lengthed inputs to purrr with nested data structures

I have two lists foo and bar, where length(foo) > length(bar). I want to apply a function length(bar) times to each element of bar and store the output of each application in is own list and store all of the applications of the function to each…
socialscientist
  • 3,759
  • 5
  • 23
  • 58
1
vote
0 answers

Why ifelse or if_else doesn't work as expected in purrr::accumulate

Actually while attempting this problem I came across a weird situation where baseR's if() else() is working as expected but neither of ifelse and if_else are working. Let me rephrase the actual problem here. The values in a vector are to be…
AnilGoyal
  • 25,297
  • 4
  • 27
  • 45
1
vote
1 answer

Why is function not properly mapping to values while using purrr::possibly?

I'm trying to use purrr::possibly() to skip over errors in my code, but for some reason, when I use possibly() here, it returns the output as if everything is an error. Here's an example: library(purrr) add_things <- function(number) { …
Evan O.
  • 1,553
  • 2
  • 11
  • 20
1 2 3
99
100