purrr Questions

5

Solved

I have some archived Slack data that I am trying to get some of key message properties. I'd done this by stupidly flattening the entire list, getting a data.frame or tibble with lists nested in som...
Streeto asked 28/10, 2020 at 18:16

2

Solved

I'd like to sample rows from a data frame by group. But here's the catch, I'd like to sample a different number of records based on data from another table. Here is my reproducible data: df <- ...
Lamee asked 15/1, 2017 at 21:50

2

Solved

I have a reprex as follows: library(dplyr) library(purrr) df1 <- data.frame( col1 = 1:5, col2 = 6:10 ) df2 <- df1 %>% mutate(col3 = 1:5) ls <- list( a = list(df1 = df1), b = l...
Kloof asked 4/7 at 6:44

4

Solved

Take this simple dataset and function (representative of more complex problems): x <- data.frame(a = 1:3, b = 2:4) mult <- function(a,b,n) (a + b) * n Using base R's Map I could do this to...
Breadthways asked 23/8, 2018 at 4:0

3

Solved

I would like to mutate new columns onto a dataframe using a function where the inputs to the function come from a vector. Like this: library(tidyverse) # sessionInfo() # R version 4.3.2 (2023-10-3...
Amatory asked 28/5 at 2:1

4

Solved

I'm trying to do something that seems very simple, but I cannot figure it out. I have a tibble like so: > df <- tibble::tribble( ~col_a, ~col_b, 1, "A", 2, "B", 3, &...
Eduard asked 24/7, 2020 at 14:33

6

Seems like a basic question and perhaps I'm just missing something obvious ... but is there any way to pluck a sublist (with purrr)? More specifically, here's an initial list: l <- list(a = "fo...
Revise asked 27/10, 2017 at 21:35

3

Solved

How can I find the path of an element in a nested list without manually digging through a list in a View? Here is an example that I can already deal with: l1 <- list(x = list(a = "no_match&...
Defant asked 17/3 at 10:19

3

Solved

I'm trying to solve the following problem in R: I have a dataframe with two variables (number of successes, and number of total trials). # A tibble: 4 x 2 Success N <dbl> <dbl> 1 28....
Bibliopegy asked 11/3, 2018 at 16:36

4

Solved

Beginner in data manipulation in R, I struggle with multi-level nested lists. Question: Is there a way to transform this dat0 3-levels list into the global dat1 dataframe below? The new fulltext c...
Origen asked 19/12, 2023 at 18:1

3

Solved

Consider this simple example mydf <- data_frame(regular_col = c(1,2), normal_col = c('a','b'), weird_col = list(list('hakuna', 'matata'), list('squash', 'banana'))) > mydf # A tibble: 2 ...
Gladysglagolitic asked 12/8, 2018 at 22:42

10

Solved

I have to following issue using R. In short I want to create multiple new columns in a data frame based on calculations of different column pairs in the data frame. The data looks as follows: df ...
Leboff asked 13/4, 2018 at 12:8

5

I have a list structured this way: x <- list(id = c("a", "b"), value = c(1,2), othervalue = c(3,4) ) I need to transform the list to this structure like this: y <- li...
Zygote asked 12/6, 2023 at 10:35

0

I have a function using nested map calls to process chunks (the inner map process a single chunk and the outer map runs through all chunks). Both processes are slow enough that it's useful to have ...
Figurine asked 7/6, 2023 at 2:10

2

Solved

I am using map function from purrr library to apply segmented function (from segmented library) as follows: library(purrr) library(dplyr) library(segmented) # Data frame is nested to create list...
Backstay asked 6/12, 2016 at 22:14

2

As part of a pipeline, I'd like to take a data frame or tibble and rename a subset of columns, specified by a vector of position indices, with the new column names as a function of their index rath...
Comrade asked 20/3, 2023 at 21:44

3

Solved

Another seemingly easy task where purrr::map2 is giving me a hard time. I have a list of data frames and I have a vector of column names. I now want to rename a certain column in each of the data f...
Stalag asked 6/3, 2023 at 8:0

3

Solved

I have a dataframe (df1) and a list of dataframes (test) like below; I want to join df1 with each of the datafraems in test and populate a new column (X), while keeping all the other records intact...
Valid asked 7/2, 2023 at 16:25

1

Solved

Consider the following Rmarkdown document: --- title: "Environments" author: "Me" date: "2023-01-13" output: html_document --- ```{r setup} library(glue) library(purr...
Solfeggio asked 13/1, 2023 at 10:15

2

Maybe I'm missing something obvious but trying to flatten a list of named lists of named lists in R (may even be more nested) into eventually one flat list. purrr and rlist seem to have tools for t...
Hypothyroidism asked 13/3, 2018 at 9:22

4

Solved

Is there a way to map to any type with purrr::map library(tidyverse) library(lubridate) df <- data_frame(id = c(1, 1, 1, 2, 2, 2), val = c(1, 2, 3, 1, 2, 3), date = ymd("2017-01-01") + day...
Imperil asked 8/4, 2017 at 9:25

5

Solved

Is there any reason why I should use map(<list-like-object>, function(x) <do stuff>) instead of lapply(<list-like-object>, function(x) <do stuff>) the output should be...
Kickapoo asked 14/7, 2017 at 10:45

6

How do I achieve row-wise iteration using purrr::map? Here's how I'd do it with a standard row-wise apply. df <- data.frame(a = 1:10, b = 11:20, c = 21:30) lst_result <- apply(df, 1, func...
Callipash asked 23/10, 2017 at 22:24

3

I am using the 'segmented' package to find break points in linear regressions in R library(tidyverse) library(segmented) df <- data.frame(x = c(1:10), y = c(1,1,1,1,1,6:10)) lm_model <- lm(y...
Tocsin asked 23/10, 2019 at 14:51

5

Solved

I have a list with many levels. I want to only keep elements at the 1st level. Example list: my_list <- list( a.1 = "some text", b.1 = NA, c.1 = integer(0), d.1 = "some text...
Captive asked 30/8, 2022 at 20:9

© 2022 - 2024 — McMap. All rights reserved.