purrr

purrr::pmap with other default inputs

≡放荡痞女 提交于 2019-12-06 06:35:39
问题 I am wondering how to use pmap() function if I have more than 3 inputs as parameters to map into a function with other default inputs. Here is a reproducible example: a=c(5, 100, 900) b=c(1, 2, 3) ablist=list(mean=a,sd=b) pmap(ablist, ~rnorm( mean=a , sd=b , n = 9)) outputs: [[1]] [1] 5.734723 99.883171 895.962561 5.346905 98.723191 903.373177 4.172267 96.424440 897.437970 [[2]] [1] 4.427977 98.348139 899.287248 4.404674 99.178516 900.983974 3.836353 101.520355 899.992332 [[3]] [1] 4.961772

Extract model summaries and store them as a new column

喜欢而已 提交于 2019-12-06 05:00:48
问题 I'm new to the purrr paradigm and am struggling with it. Following a few sources I have managed to get so far as to nest a data frame, run a linear model on the nested data, extract some coefficients from each lm, and generate a summary for each lm. The last thing I want to do is extract the "r.squared" from the summary (which I would have thought would be the simplest part of what I'm trying to achieve), but for whatever reason I can't get the syntax right. Here's a MWE of what I have that

why does map_if() not work within a list

走远了吗. 提交于 2019-12-06 01:47:18
问题 Please help me 1) Why does map_if not work within a list 2) Is there a way to make it work 3) If not, what are the alternatives Thanks in advance. library(dplyr) library(purrr) cyl <- split(mtcars, mtcars$cyl) # This works map_if(mtcars, is.numeric, mean) # This does not work map_if(cyl, is.numeric, mean) 回答1: Because you need to map to one lever lower, the columns are at level 2. So you can do: map(cyl, ~map_if(., is.numeric, mean)) Or: map(cyl, map_if, is.numeric, mean) Without the if one

map a vector of characters to lm formula in r

旧时模样 提交于 2019-12-06 01:37:19
问题 I'm trying to make a list of lm object using purrr::map. use mtcars as an example: vars <- c('hp', 'wt', 'disp') map(vars, ~lm(mpg~.x, data=mtcars)) error: Error in model.frame.default(formula = mpg ~ .x, data = mtcars, drop.unused.levels = TRUE) : variable lengths differ (found for '.x') I also tried: map(vars, function(x) {x=sym(x); lm(mpg~!!x, data=mtcars)}) I got error message: Error in !x : invalid argument type Can anyone tell what I did wrong? Thanks in advance. 回答1: The usual way is

passing ellipsis arguments to map function purrr package, R

被刻印的时光 ゝ 提交于 2019-12-05 20:57:06
I want to use ellipsis parameters inside map function of purrr package. this is a toy example: f1<-function(x,a=NA,b=NA,prs=seq(0, 1, 0.25),SW=T){ if(SW){ res<-data.frame(name1=a,name2=b,t(quantile(x, prs, na.rm = T)), mean=mean(x, na.rm = T), sd=sd(x, na.rm = T), NAs=length(x[is.na(x)]),n=length(x[!is.na(x)]),SWp=shapiro.test(x)$p.value,stringsAsFactors =F) }else { res<-data.frame(name1=a,name2=b,t(quantile(x, prs, na.rm = T)), mean=mean(x, na.rm = T), sd=sd(x, na.rm = T), NAs=length(x[is.na(x)]),n=length(x[!is.na(x)]),stringsAsFactors =F) } return(res) } f1(c(NA,rnorm(25),NA),SW=F) f1(c(NA

recode/replace multiple values in a shared data column to a single value across data frames

放肆的年华 提交于 2019-12-05 16:00:27
I hope I haven't missed it, but I haven't been able to find a working solution to this problem. I have a set of data frames with a shared column. These columns contain multiple and varying transcription errors, some of which are shared, others not, for multiple values. I would like replace/recode the transcription errors (bad_values) with the correct values (good_values) across all data frames. I have tried nesting the map*() family of functions across lists of data frames, bad_values, and good_values to do this, among other things. Here is an example: df1 = data.frame(grp = c("a1","a.","a."

Joining list of data.frames from map() call

折月煮酒 提交于 2019-12-05 09:31:15
Is there a "tidyverse" way to join a list of data.frames (a la full_join() , but for >2 data.frames)? I have a list of data.frames as a result of a call to map() . I've used Reduce() to do something like this before, but would like to merge them as part of a pipeline - just haven't found an elegant way to do that. Toy example: library(tidyverse) ## Function to make a data.frame with an ID column and a random variable column with mean = df_mean make.df <- function(df_mean){ data.frame(id = 1:50, x = rnorm(n = 50, mean = df_mean)) } ## What I'd love: my.dfs <- map(c(5, 10, 15), make.df) #%>% # <

How do pipes work with purrr map() function and the “.” (dot) symbol

℡╲_俬逩灬. 提交于 2019-12-05 08:53:59
When using both pipes and the map() function from purrr, I am confused about how data and variables are passed along. For instance, this code works as I expect: library(tidyverse) cars %>% select_if(is.numeric) %>% map(~hist(.)) Yet, when I try something similar using ggplot, it behaves in a strange way. cars %>% select_if(is.numeric) %>% map(~ggplot(cars, aes(.)) + geom_histogram()) I'm guessing this is because the "." in this case is passing a vector to aes(), which is expecting a column name. Either way, I wish I could pass each numeric column to a ggplot function using pipes and map().

How can I speed up spatial operations in `dplyr::mutate()`?

蹲街弑〆低调 提交于 2019-12-05 08:17:10
I am working on a spatial problem using the sf package in conjunction with dplyr and purrr . I would prefer to perform spatial operations inside a mutate call, like so: simple_feature %>% mutate(geometry_area = map_dbl(geometry, ~ as.double(st_area(.x)))) I like that this approach allows me to run a series of spatial operations using %>% and mutate . I dislike that this approach seems to significantly increase the run-time of the sf functions (sometimes prohibitively) and I would appreciate hearing suggestions about how to overcome this speed loss. Here is a reprex that illustrates the speed

Add multiple output variables using purrr and a predefined function

a 夏天 提交于 2019-12-05 08:06:24
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 add 2 new columns in a vectorised fashion: ns <- 1:2 x[paste0("new",seq_along(ns))] <- Map(mult, x["a"], x["b"], n=ns) x # a b new1 new2 #1 1 2 3 6 #2 2 3 5 10 #3 3 4 7 14 purrr attempt via pmap gets close with a list output: library(purrr) library(dplyr) x %>% select(a,b) %>% pmap(mult, n=1:2) #[[1]] #[1] 3 6 # #[[2]] #[1] 5 10 # #[[3]] #[1] 7 14 My attempts from here with pmap_dfr etc all seem to error out