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

Use purrr::map to merge list within a tibble with a data frame

I am using purrr and want to use map or on a list list1 within a tibble my_tibble in combination with an external dataframe my_dataframe, the idea is for my_dataframe to be merged to every item of list1: library(dplyr) library(purrr) df1 <-…
AEM
  • 919
  • 1
  • 9
  • 22
1
vote
2 answers

Reconcile dataset *column types* (formats) using a dictionary/list in R/dplyr

Following on the renaming request #67453183 I want to do the same for formats using the dictionary, because it won't bring together columns of distinct types. I have a series of data sets and a dictionary to bring these together. But I'm struggling…
daaronr
  • 507
  • 1
  • 4
  • 12
1
vote
1 answer

Reconstructing dataframes with map_dfr() in R

I am having the toy iris dataset split into 3 parts with respect to the classes they have. setosa <- read_csv("iris-setosa.csv") %>% mutate(Species = "setosa") versicolor <- read_csv("iris-versicolor.csv") %>% mutate(Species =…
Ranji Raj
  • 778
  • 4
  • 18
1
vote
2 answers

R - Discard/Keep elements in a list if children elements exist

I want to keep all the elements of a list where the children contain a certain named element. For example, in this I only want the elements that contain the child "phone" (elements, 1, 3 and 5). my_list <- list(list(phone = "123-124-1234", …
Jeff Parker
  • 1,809
  • 1
  • 18
  • 28
1
vote
1 answer

Extract each element in a list and store it in objects

I have the following datafram: ï..Employee_Name PositionID Position State Zip 1: Adinolfi, Wilson K 19 Production Technician I MA 1960 2: Ait Sidi, Karthikeyan 27 Sr. DBA MA…
Taren Shaw
  • 89
  • 6
1
vote
3 answers

R: create a data.frame or data.table from a list and unpacked list of lists

I have a list and a list of lists and would like to create a data.frame or data.table. Here is the list: head(stadte_namen) [1] "Berlin" "Hamburg" "München" and a list of lists > head(result) [[1]] min max x…
Andreas
  • 397
  • 4
  • 18
  • 37
1
vote
3 answers

Find maximum value for x for a polynomial function

I am using a simple polynomial to fit a curve. poly <- function(a, b, c, x) a * x^2 + b * x + c I'd like to find the value of x that results in the maximum value of the curve. Currently I create a grid with a range of x from 20000 to 50000, run the…
Nazer
  • 3,654
  • 8
  • 33
  • 47
1
vote
2 answers

Create multiple columns in data frame based on another column

I would like to update a data frame to add 10 columns with values based on another column Starting with this df <- data.frame(ID = 1:3, name = c("Bob", "Jim", "Fred"), endValue= c(3, 7, 4)) And ending with…
Chris
  • 1,449
  • 1
  • 18
  • 39
1
vote
2 answers

Rectangling nested lists with different names/indecies

My data input is comes from JSON data and names in the list are the keys from the key/value pairs of the JSON. So it looks like this: # Dummy data doc1 <- list(type = "HTML", garbage = "blahblah", `1 - 28` = list(food =…
Jeff Parker
  • 1,809
  • 1
  • 18
  • 28
1
vote
2 answers

apply multiple logical functions to string to obtain a single final logical vector

I am looking for a way to apply two different logical conditions (an inclusion and an exclusion statement) to a string, and obtain a logical vector as output: I was able to do it with the following…
GuedesBF
  • 8,409
  • 5
  • 19
  • 37
1
vote
2 answers

R Map through list of data.frames and change column types

I am pretty new to R and kinda desperate, since I have been working on this problem for quite some time now. I have a list of dataframes: df1 <- data.frame(na = c("bla", "foo", "bar", "baz"), tmp = c(1,2,5,6), tf = c(2.2, 3.4, 5.6, 7.7)) df2 <-…
Effigy
  • 145
  • 7
1
vote
1 answer

How to create tidy correlations using nested dataframes?

This question has been partially answered previously (e.g., here), but – as far as I can tell – there is no full answer using a reproducible example. I would like to select variables by name from a nested data frame, calculate pairwise correlations,…
nicholas
  • 903
  • 2
  • 12
1
vote
1 answer

R return dataframe from vector of dataframe names

I have a vector (test_vector), and it contains a string; that string is also the name of a dataframe. I need a function that will allow me to display the dataframe knowing given a value in test-vector: test_vector = "x" x =…
evidently
  • 43
  • 4
1
vote
1 answer

How can I retain column names when unnesting?

I am calculating summary statistics based on nested dataframes, and I am adding the statistics in unnested columns. Here is a toy example: library(purrr) library(dplyr) library(tidyr) df <- tribble( ~data, tibble(pop = c(1, 2, 3)), …
nicholas
  • 903
  • 2
  • 12
1
vote
1 answer

How can I use map2 and rename_with to modify nested column names?

I am trying to rename a set of columns in nested dataframes based on the values of an unnested column. Here is a simplified example of the dataset: library(tidyverse) df_pre <- tribble( ~year, ~data, 1970, tibble(GEOID_1970 = 1, TOTPOP_1970 =…
nicholas
  • 903
  • 2
  • 12
1 2 3
99
100