3,555 questions
2
votes
1
answer
81
views
Using marginaleffects inside a purrr::map
I use the tidyverse suite to estimate 6 linear models as a function of empirical subgroups and different functional forms
library(tidyverse)
library(magrittr)
library(marginaleffects)
library(...
Best practices
1
vote
6
replies
81
views
Using local functions in mirai & purrr
I am working on a project that has some pretty heavy computations that I want to parallelize. I want to use purrr and mirai. I have a ton of functions defined that are used in the computations that ...
4
votes
2
answers
103
views
mutate(across(map())): Any way to include the column name in a .progress message from map()?
If you want to call a function that returns multiple values on a column of a data.frame, and append these values as new columns to the data.frame, you can mutate(map()) |> unnest_wider(). If it's ...
0
votes
1
answer
88
views
renaming columns within `map` dynamically using the values from a certain row
Taking the following data:
library(tidyverse)
df <- data.frame(id = c("a", "a", "a", "b", "b", "b"),
x = c(NA, "...
2
votes
2
answers
371
views
`purrr::in_parallel` extremely slow for rowwise operation on data.frames
I was excited when I read about the latest update of purrr (1.1.0) with its in_parallel capabilities.
I just happen to have a time-consuming data task that runs for several minutes, because:
my data ...
2
votes
2
answers
137
views
Writing an Excel file in combination with purrr::map leads to an error
I have a simple list like
library(tidyverse)
library(openxlsx)
mtcars |>
group_split(cyl)
And now I want to save each list as an Excel file to the folder specified in cyl like so:
mtcars |>
...
1
vote
1
answer
124
views
R Reticulate does work with for loop but not purrr::map2
I want to remove passwords from several Excel files, so I specified a data frame with paths and passwords and then added some python code running with reticulate.
When I pass that function to a for ...
0
votes
1
answer
87
views
Async calls to CLI tool in separate folders
I like to call a command line utility from R and parallelise the tool by calling several copies of it in different folders. Each call can have quite different runtimes, so I'd like to do this in an ...
0
votes
2
answers
83
views
how to extract dataframe name from a list using map
I tried to use 'map' to filter a list of dataset. how can I extract the name of dataframe, which is 'YYY' in my code.
library(dplyr)
library(purrr)
df1 <- data.frame(var1=seq(1:10), var2=seq(1:10),...
0
votes
1
answer
79
views
Extracting & Joining Values from a Nested List to a single column in R [closed]
I have a deeply nested list that I am trying to extract into a simple df.
Sample List
df <- tibble::tribble(
~division, ~id, ...
1
vote
3
answers
155
views
Change output of the `purrr::map` function
I have this small function and want to apply it to a dataset using purrr::map(). It does what I expect it to do, but after the output it also prints a list of NULL, which I don't want. I would like to ...
1
vote
1
answer
95
views
Error handling for tidyr hoist in API call dplyr pipe when column type changes between calls
I'm working with the Jira Native API to bring in dashboard data to R for rectangularizing.
The max results that can be returned each call is 100. I'm working with a small dashboard atm but am thinking ...
0
votes
0
answers
42
views
The execution of vectorized functions after purrr discarding the invoke function (tibble, map and exec) [duplicate]
In the process of learning this part:
vectorised functions
The current version of purrr has deprecated the invoke_map function, what is the problem with this latter code? I am really confused.
sim <...
1
vote
1
answer
86
views
Using httr2::last_response() in conjunction with purrr::possibly()
I have an issue when working with httr2::last_response() and purrr::possibly() and I can't quite figure out how to resolve it, as other posts involving either httr2 or purrr::possibly doesn't really ...
1
vote
4
answers
127
views
How can I select specific items in a list that are not at the top level? [duplicate]
I'm trying to select specific items in a list that are not in the top level, and everything I can think of doesn't work the way I would expect. Here is a dummy list where what I do NOT want is item &...
-1
votes
1
answer
64
views
Imap with multiple ifelse for 3 tiems 4 subplots and facets rows and columns in R
Based on this post for gglikert. There the group1 column had 2 levels and group2 has 3. Now if i implement it with 3 levels in group1 and 4 levels in group2
library(ggstats)
library(dplyr)
library(...
0
votes
1
answer
46
views
How to aggregate over multiple data.frames using something like rbind R [duplicate]
I have a bunch of dataframes that contain individual-level person data for complex survey weight adjusted survey data at a state level. Say one for each state:
df_1, df_2, ..., df_50
I have a ...
9
votes
2
answers
1k
views
Use purrr::accumulate with condition
Below is a reproducible example with a test dataframe:
dat<- structure(list(A = c(1.3, 1.5, 1.6, 1.2, 1.1, 1.2),
B = c(0.25, 0.21, 0.21, 0.15, 0.26, 0.17)),
...
2
votes
3
answers
98
views
R: Conditional suppression of values across multiple rows
This question is similar to this question, but the difference in this case is to suppress data row-wise. The suppression is done in each row by replacing any value less than 6 in each row with -1 ...
2
votes
4
answers
135
views
R: Conditional suppression of values across multiple columns
I have a huge data. I like to suppress data across multiple columns by replacing any value less than 6 in each column with -1 such that if in a column, there is only one suppressed value, I want to ...
1
vote
2
answers
70
views
using purrr instead of for loop and updating object recursively
Given a data frame such as the following:
dat <- data.frame(
plotType = c("RP", "FP"),
turbType = c("3;8", "5;7"),
turbSize = c(2.4, "3.6;...
1
vote
0
answers
51
views
Apply a function to many data frames
The following is an example of a question that has been asked several times in the past, but I have not seen a fully satisfactory answer. I would like to know if newer strategies have been developed, ...
4
votes
3
answers
133
views
How to append the name of a sublist vector as a named element within the vector
I have a list that contains web-traffic details.
In the process of rectangling the lists, I'd like to append a value to each vector in the named sublists which is the actual name of the sublist vector....
3
votes
1
answer
72
views
Map strings in one vector, which are substrings of longer strings in another vector, to the same substrings in the longer strings
In my data, the strings in one column (TCU_clean) are substrings in another column (TCU_clean_collpsd); in a third column (w_c7_collpsd), which is the key target column, the same substrings appear but ...
4
votes
2
answers
104
views
How to combine elements from a list and ensure only the latest record is used when applicable?
I am working with a series of data.frames in a list, where each round adds a new year and drops the last year, as shown in the picture below:
Here is the R code for the minimal reproducible example:
...
0
votes
1
answer
58
views
Using map() with GAM model
I have a code that works if I am using LM but not if I am using a GAM. I guess the that for whatever reason LM looks inside object for the variable and GAM just looks at the names of the object but I ...
0
votes
4
answers
98
views
Fill in column based on condition with another column in R [closed]
I have the following input table:
input <- structure(
list(individual = c(1, 2, 3, 4),
age = c(20, 34, 29, 30),
earnings_2020 = c(0, 0, 1, 0),...
2
votes
1
answer
77
views
glue not find .x in map together with partial function
I have modified glue using purrr::partial and it works fine except when I try to using it together with purrr::map. There are multiple workaround but I'm confused on why this does not work?
library(...
2
votes
2
answers
80
views
ggplot2 loop over two columns to create plots in groups
I have a dataset in which one of the variables is states and the other is districts (that are located in each state), and the remaining are numerical variables. There are 32 states, and 712 districts ...
1
vote
0
answers
112
views
Why is calling an external function in pmap() slower than executing code directly?
The following minimal example illustrates it:
dfr <- data.frame(x=1:1e6, y=1:1e6)
f <- function(x,y) x+y
# faster
system.time(
for (i in 1:50) purrr::pmap(dfr, \(x,y) x+y)
)
# slower
system....
0
votes
1
answer
133
views
How to cut down on argument redundancy in purrr::map
Map statements are just awesome, but their inputs often feel needlessly redundant. For example, in the block below, it seems silly to have to re-list the variables I've already requested from the ...
1
vote
1
answer
89
views
R dplyr function ceases to work when using map to iterate over vector of column names
I've got a large dataset and want to apply a custom function over each of the columns.
I've written the function and it works when applied as a one-off on one column of the target dataset.
However, ...
2
votes
1
answer
101
views
How to combine CLI message with purrr family functions
I am wondering how to convert this example code to purrr style by using the walk() function instead?
library(cli)
n <- 10
cli_alert_info("About to start downloads of {n} file{?s}")
i &...
1
vote
1
answer
246
views
R purrr package map() vs pmap() difference
I am trying to understand the behavior of purrr::map() vs purrr::pmap() for the following use case. I was expecting the same result, but looks like purrr::map() iterates only once through the list.
# ...
1
vote
1
answer
62
views
Send multiple attachments with gmail in R
I have multiple lists which look like this:
list(structure(list(mail = c("[email protected]", "[email protected]", "[email protected]",
"[email protected]", "[email protected]", "yo@yo....
2
votes
2
answers
121
views
Execute a list of purrr-style lambda formulas on a data frame
The following toy data has 5 variables, X1 to X5.
set.seed(123)
df <- data.frame(matrix(rnorm(500), 100, 5))
I want to perform specific operations on specific variables, using a named list of ...
4
votes
4
answers
123
views
How to recycle shorter vectors in map2() without using rep()?
I’m trying to run the following code:
xx <- 1:12
yy <- 1:3
map2(xx, yy, ~ .x / .y)
However, I encountered an error. I assumed that map2() would automatically recycle the shorter vector (i.e., ...
1
vote
3
answers
125
views
`reduce` in purrr with `+` but ignore missing value in R
How can I use reduce function together with lag to obtain the summation but at the same time ignore missing values?
a <- data.frame( b = c(1, 2, NA, 3, 4, 5))
a %>% mutate(addition = reduce(map(...
1
vote
1
answer
71
views
Name new variables with purrr::map()
I am trying to learn some basic purrr. Take this toy example - how do I essentially append "rank" to the current variable name as it's created using map_df?
library(tidyverse)
# generate ...
1
vote
0
answers
119
views
How to replace this for loop with purrr::map
I'm am trying to become more familiar with purrr rather than using for loops. I have some toy data (from a real problem) where I have several clinical visit values in one cell. I need to split these ...
1
vote
0
answers
71
views
Best way to save multi-page graphs in R
Hi so I make a lot of graphs and was wondering if I was doing it in a fast, efficient way. I need to make visualizations commonly that are commonly ~2000 pages long (one for each unit of analysis)
I ...
1
vote
2
answers
154
views
Problem using purrr to map a list of variables into a function
I am working with the unvotes package, the objective is "Using purrr functionality, find out which three countries on average agreed with the US the most from the year 2000 on". I first ...
3
votes
1
answer
103
views
Tidyevaluation pattern to map over selected columns
outer_func uses tidy evaluation for col_rfs and dflt_flag, i.e. it is possible to do col_rfs = dplyr::starts_with("col").
library(magrittr)
outer_func <- function(dat, col_rfs, dflt_flag)...
2
votes
4
answers
130
views
Given a vector v and fn f, how do I generate the matrix {v, f(v), f(f(v)), ... f^k(v)}?
I have a vector x and a function f. I need to generate the matrix whose first column is v, second column is f(v), and so on up to the final column f^k(v). In a functional language I could use an ...
0
votes
1
answer
73
views
How to write custom functions that you can apply to columns in purrrr [closed]
I need to apply a custom function to a set of columns in a dataset and return a list. I can do this in lapply() but I am trying to work with purrr.
Toy data. Three factor variables
df <- data.frame(...
2
votes
1
answer
87
views
Exporting summary.betareg and summary.gam from list to csv
I have two types of models (GAM and beta regression) that I am running for my data and I am using a lapply function to iterate over multiple variables. I am encountering an error when trying to export ...
0
votes
1
answer
52
views
additional arguments to purrr:map don't work as expected
I'm using the purrr::map function to iterate over several columns and tidy the result. for a short example, I provide the following code:
library(tidymodels)
library(broom)
> penguins %>%
+ ...
5
votes
5
answers
154
views
Convert a list into a tibble with nested columns
I would like to convert a list like this into a tibble.
lst <- list(
"A"=list(
"Category"="A",
"Team"=c("x"),
"City"="...
0
votes
1
answer
63
views
Function works with plan(sequential) but not plan(multisession)
Here is my code :
plan(multisession,workers=detectCores()-2)
future_map_dfr(.x= Liste_model[1:2],.f = summaryModel, df = DF_MODEL_TRAIN, df_test = DF_MODEL_TEST, df_global = DF_MODEL_GLOBAL, .id = &...
6
votes
2
answers
139
views
Filter list of lists with purrr::keep
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 = ...