purrr

iteratively apply ggplot function within a map function

送分小仙女□ 提交于 2020-01-14 19:01:30
问题 I would like to generate a series of histograms for all variables in a dataset, but I am clearly not preparing the data correctly for use in the map function. library(tidyverse) mtcars %>% select(wt, disp, hp) %>% map(., function(x) ggplot(aes(x = x)) + geom_histogram() ) I can accomplish this task with a for loop (h/t but am trying to do the same thing within the tidyverse. foo <- function(df) { nm <- names(df) for (i in seq_along(nm)) { print( ggplot(df, aes_string(x = nm[i])) + geom

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

元气小坏坏 提交于 2020-01-13 11:23:10
问题 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

apply a function to a parameter grid and return a list of lists in purrr

别来无恙 提交于 2020-01-13 02:53:17
问题 I often use lists of lists to apply a function (often a model call) onto a grid of parameters. Here is an example with paste as the ultimate function: library(tidyverse) #purrr a=c("A", "B", "C") %>% set_names %>% map(function(x){ c("m0", "m1") %>% set_names %>% map(function(y){ c("absolute", "relative") %>% set_names %>% map(function(z){ paste(x,y,z) }) }) }) a$A$m0$absolute #exact expected output I am looking for a way to get the exact same result with a simpler call, probably by using

purrr map a t.test onto a split df

…衆ロ難τιáo~ 提交于 2020-01-11 20:23:11
问题 I'm new to purrr, Hadley's promising functional programming R library. I'm trying to take a grouped and split dataframe and run a t-test on a variable. An example using a sample dataset might look like this. mtcars %>% dplyr::select(cyl, mpg) %>% group_by(as.character(cyl)) %>% split(.$cyl) %>% map(~ t.test(.$`4`$mpg, .$`6`$mpg)) This results in the following error: Error in var(x) : 'x' is NULL In addition: Warning messages: 1: In is.na(x) : is.na() applied to non-(list or vector) of type

purrr map a t.test onto a split df

大兔子大兔子 提交于 2020-01-11 20:23:06
问题 I'm new to purrr, Hadley's promising functional programming R library. I'm trying to take a grouped and split dataframe and run a t-test on a variable. An example using a sample dataset might look like this. mtcars %>% dplyr::select(cyl, mpg) %>% group_by(as.character(cyl)) %>% split(.$cyl) %>% map(~ t.test(.$`4`$mpg, .$`6`$mpg)) This results in the following error: Error in var(x) : 'x' is NULL In addition: Warning messages: 1: In is.na(x) : is.na() applied to non-(list or vector) of type

In nested data frame, pass information from one list column to function applied in another

会有一股神秘感。 提交于 2020-01-11 12:33:36
问题 I am working on a report for which I have to export a large number of similar data frames into nice looking tables in Word. My goal is to achieve this in one go, using flextable to generate the tables and purrr / tidyverse to apply all the formatting procedures to all rows in a nested data frame. This is what my data frame looks like: df <- data.frame(school = c("A", "B", "A", "B", "A", "B"), students = c(round(runif(6, 1, 10), 0)), grade = c(1, 1, 2, 2, 3, 3)) I want to generate separate

In nested data frame, pass information from one list column to function applied in another

久未见 提交于 2020-01-11 12:33:13
问题 I am working on a report for which I have to export a large number of similar data frames into nice looking tables in Word. My goal is to achieve this in one go, using flextable to generate the tables and purrr / tidyverse to apply all the formatting procedures to all rows in a nested data frame. This is what my data frame looks like: df <- data.frame(school = c("A", "B", "A", "B", "A", "B"), students = c(round(runif(6, 1, 10), 0)), grade = c(1, 1, 2, 2, 3, 3)) I want to generate separate

How to create a list of list and then perform a vectorised function over it

本秂侑毒 提交于 2020-01-11 03:49:07
问题 I'm looking for two specific help point in this request 1) how to create a list of list given my data base (all.df) below 2) how to vectorise a function over this list of list I'm trying to generate a forecast at a customer / product level using the Prophet library. Im struggling to vectorise the operation. I currently run a for loop, which I want to avoid and speed-up my calculations. Data for the analysis set.seed(1123) df1 <- data.frame( Date = seq(dmy("01/01/2017"), by = "day", length.out

Supplying multiple groups of variables to a function for dplyr arguments in the body

六眼飞鱼酱① 提交于 2020-01-10 05:05:08
问题 Here is the data: library(tidyverse) data <- tibble::tribble( ~var1, ~var2, ~var3, ~var4, ~var5, "a", "d", "g", "hello", 1L, "a", "d", "h", "hello", 2L, "b", "e", "h", "k", 4L, "b", "e", "h", "k", 7L, "c", "f", "i", "hello", 3L, "c", "f", "i", "hello", 4L ) and the vectors, I want to use: filter_var <- c("hello") groupby_vars1 <- c("var1", "var2", "var3") groupby_vars2 <- c("var1", "var2") joinby_vars1 <- c("var1", "var2") joinby_vars2 <- c("var1", "var2", "var3") 2nd & 5th, and 3rd & 4th

Trying to unnest broom::augment data, but R “can't cast”

给你一囗甜甜゛ 提交于 2020-01-06 05:17:46
问题 I can't reproduce the data here, but I'm hoping I'm making an obvious mistake. I am trying to get residuals from all the of the models I fit with purrr::map. My code looks like this: df %>% group_by(group) %>% nest() %>% mutate(model = map(data, fit_mod), model_data = map(model, broom::augment)) %>% ungroup()%>% unnest(c(data, model_data)) I get an error related to the title of one of the coefficients in my model: Error: Can't cast model_data$poly.Actual_Population..2..raw...TRUE. to model