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

Applying lmer function over several groups and several outcomes in data frame R

I have a data frame that looks a bit like this: example <- tribble( ~PERSON_ID, ~RANGE, ~outcome1, ~outcome2, ~outcome3, ~type, "1", "PRE", 1, 2, 3, "type1", "1", "POST", 2, 3, 4, "type1", "2", "PRE", 3, 4, 5, "type2", "2", "POST", 5, 6,…
Dr Wampa
  • 443
  • 3
  • 8
1
vote
2 answers

Categorise a variable in a list using the purrr::map_df() function

I have a list of datasets that I obtained from multiple imputation. I would like to now recategorise a variable within this list of datasets. I have tried using the map function from purrr, I have not had much luck with this as per the code…
Mohamed Yusuf
  • 390
  • 1
  • 11
1
vote
2 answers

Purr or loop over table to automate the binding rows process

I have the following table: df <- tribble(~name, ~label, ~year, ~id, "base1.dta", "Generated biographical information", 1990, "gbi", "base2.dta",…
Paula
  • 497
  • 2
  • 8
1
vote
5 answers

R - using map to apply a list function to dataframe column and create new columns with elements of the list

I have a dataframe with and id column and an eats column, and a separate food list. I want to process the dataframe so that a column is added for each food in the food list which is populated with 1 if the food is present in eats and 0…
Moshee
  • 544
  • 1
  • 3
  • 16
1
vote
1 answer

"Error: Argument 1 must be a data frame or a named atomic vector. " for `purrr::map_dfr()`

I was trying to run a regression models on multiple subgroups of a dataframe using purrr::map_dfr(), but somehow I get this somewhat weird error. library(dplyr) library(purrr) # Create some data test_df = map_dfr(seq_len(5), ~mtcars, .id =…
Miao Cai
  • 902
  • 9
  • 25
1
vote
1 answer

Parse one column of json and bind with other column to make dataframe

I have data that takes the format: have <- structure(list(V1 = c(4L, 28L, 2L), V2 =…
Eric Green
  • 7,385
  • 11
  • 56
  • 102
1
vote
1 answer

Map/mapply with all possible combinations of two lists

I am new to using R and apply and I am trying to download a set of .csv files from a website. I want to download the years 2004 and 2005 (as an example; I want more years in fact) of three countries, Guatemala (GT), El Salvador (SV), and Honduras…
jpugliese
  • 261
  • 1
  • 11
1
vote
1 answer

R - Keeping column names after merging multiple .xlsx files

I want to merge columns of multiple xlsx files (about 100) which all have the same structure, meaning the all have 5 rows and 2 columns. I'm using the answer provided here (with a slight edit to merge the columns instead of…
maryam
  • 101
  • 5
1
vote
2 answers

How to identify df in a list that only have ID but nothing else in it

I have a list of list like below: lst<- list(`101-01-101` = list(Demographics = structure(list(SubjectID = c("SubjectID", "101-01-101"), BRTHDTC = c("BRTHDTC", "1953-07-07"), SEX = c("SEX", "Female")), row.names = c(NA, -2L), class = c("tbl_df",…
Stataq
  • 2,237
  • 6
  • 14
1
vote
2 answers

How to loop through multiple columns with same filters?

I'm trying to see if I can use a loop using apply or purrr functions to loop through columns to filter the same data value. For example, I'm looking for a way to loop through columns Q2_1:Q2_10. Currently, I'm doing this in a highly inefficient…
writer_typer
  • 708
  • 7
  • 25
1
vote
1 answer

modify_at to remove NA values in each element in a list

I have a big list of small datasets like this: >> my_list [[1]] # A tibble: 6 x 2 Year FIPS 1 2015 12001 2 2015 51013 3 2015 12081 4 2015 12115 5 2015 12127 6 2015 42003 [[2]] # A tibble: 9 x 2 Year FIPS 1…
Ariel
  • 395
  • 1
  • 14
1
vote
2 answers

Using purrr to map calculations to different networks via tidygraph

The tidygraph package is really fantastic for computing network statistics. However, I have a network with a temporal dimension, and I'd like to calculate network statistics (e.g. centrality) over each of those networks. (E.g. calculate centrality…
jhersh
  • 11
  • 1
1
vote
2 answers

How to use accumulate function to calculate 2 intertwining variables

Hello my dear friends/ teachers/ fellow R users, I have been grappling with the idea of using accumulate family of functions from purrr for a while and recently I've come across a very tricky situation when we would like to calculate 2 new variables…
Anoushiravan R
  • 21,622
  • 3
  • 18
  • 41
1
vote
1 answer

R: loop through 2 columns to subset a data.table or data.frame

I want to subset a data.table or data frame by two columns using a loop. I am wondering how to do it with a nice way using purrr or dplyr. Here is my example: library(data.table) # my example data DF <- as.data.table(cbind.data.frame(x = c(100.01,…
Wang
  • 1,314
  • 14
  • 21
1
vote
1 answer

Purrr map over splitted training-dataframe to get auroc for each model

So I have this, kind of nonsense, example. I split up the iris-dataframe into their species and try to predict if a observation is of species setosa. library(proc) iris[["setosa"]] = ifelse(iris$Species == "setosa", TRUE, FALSE) sample <-…
Lenn
  • 1,283
  • 7
  • 20