purrr

purrr::pmap with dplyr::mutate

久未见 提交于 2019-12-11 11:57:51
问题 I have a function which takes multiple inputs and creates multiple outputs. For example: example_fun = function(a,b){ x = a+b y = a-b return(list(x=x, y=y)) } How can I use dplyr::mutate to evaluate this function on each row of a dataframe? Turn df = expand.grid(a=c(7,8), b=c(9,10)) df a b 1 7 9 2 8 9 3 7 10 4 8 10 into a b x y 1 7 9 16 -2 2 8 9 17 -1 3 7 10 17 -3 4 8 10 18 -2 this following code almost accomplishes it: df = df %>% mutate(outputs = pmap(list(a,b), example_fun)) %>% unnest()

Apply function over every entry one table to every entry of another

淺唱寂寞╮ 提交于 2019-12-11 11:56:45
问题 I would like to apply a function, bandedlossfn to all entries in loss.tib using every item in bandstib library(tidyverse) set.seed(1) n <- 5 loss <- rbeta(n, 1, 10) * 100 loss.tib <- loss %>% as_tibble %>% mutate(loss = value) %>% mutate(lossid = row_number()) %>% select(lossid, loss) bandstib <- tibble(bandid = seq(4), start = seq(0, 75, by = 25), end = seq(25, 100, by = 25)) bandedlossfn <- function(loss, start, end) { pmin(end - start, pmax(0, loss - start)) } As per the answer below the

R: using rvest and purrr:map_df to build a data frame: how to deal with incomplete input [duplicate]

↘锁芯ラ 提交于 2019-12-11 08:45:07
问题 This question already has answers here : Scraping with rvest - complete with NAs when tag is not present (4 answers) Closed 7 months ago . I am webscraping webpages with rvest and turning the collected data into a dataframe using purrr::map_df . The problem I ran into is that not all webpages have content on every html_nodes that I specify, and map_df is ignoring such incomplete webpages. I would want map_df to include said webpages and write NA wherever a html_nodes does not match content.

looping over a list of filter expressions: problem with NSE in map2 call within mutate

試著忘記壹切 提交于 2019-12-11 07:38:43
问题 I have defined a list of expressions containing arguments I want to pass to a dplyr::filter call. library(tidyverse) # using tidyr 1.0.0 cond_filter <- list(expr(1 > 0), # condition to select all rows expr(Species == "setosa"), expr(Species != "virginica")) I further have a data frame that I put into a list-column and which I then expand by the number of filter expressions in said list. iris_nest <- iris %>% nest(data = everything()) %>% expand_grid(., filters = cond_filter) In a last step I

Create new variables based on another df

旧巷老猫 提交于 2019-12-11 06:56:37
问题 I'm trying to up my R game, and I clearly need some guidance. I wanna create a lot of variables (93, to be exact), but I wanna do that the smart way. But I'm stuck. My problem: a dataframe (df) containing some variables, including the "main" one, which contains the stems of my description variable. Another dataframe (reference), more of a reference table, containing two columns - the category and the regex necessary to identify it; I kept only 3 entries, but its 93 originally. The code:

Unnest all columns of nested tibble to a list of tibbles

前提是你 提交于 2019-12-11 06:25:33
问题 I am fitting a model to each group in a dataset. I am nesting the data by the grouping variable and then using map to fit a model to each group. Then I store the tidied model information as columns in a nested tibble. I'd like to save each of these columns as its own file, this example saves them as sheets in a excel workbook. Is there a way to not to unnest each column individually as a new tibble? Can all columns be unnested at once to a new list of tibbles? One that can be used in other

if-statement in a function using purrr and dplyr (List Column Workflow) in R

萝らか妹 提交于 2019-12-11 05:59:07
问题 I am trying to estimate differences in means for two hospitals, A and B. Every hospital has different "groups" and I have given them group 1 and 2 in the simulated data set. That is I want to test difference in means between hospital A and B within group 1 and group 2 and in addition I have more than one variable (e.g. value1 and value2). So I have to test value1 between hospital A and B within the groups 1 and 2. Even though I specify method=1 in the call at the end I get the third method

Using pmap with a to apply different regular expressions to different variables in a tibble?

匆匆过客 提交于 2019-12-11 05:14:26
问题 This question is very similar to Using pmap to apply different regular expressions to different variables in a tibble?, but differs because I realized my examples were not sufficient to describe my problem. I'm trying to apply different regular expressions to different variables in a tibble. For example, I've made a tibble listing 1) the variable name I want to modify, 2) the regex I want to match, and 3) the replacement string. I'd like to apply the regex/replacement to the variable in a

ggplot2 and purrr: loop with split() and refer to index value

空扰寡人 提交于 2019-12-11 04:11:33
问题 I'm trying to loop many plots using ggplot2 and purrr::map() combined with split(). I can get the plots working fine, but I'm having trouble extracting the split value to use as a title. Token data: y=tibble(SplitVar=rep(c('A','B','C'),3), Variable=c(rep('A',3),rep('B',3),rep('C',3)), Value=runif(n=9)) The plots I want are generated by: y%>%split(y$SplitVar)%>%map(~ggplot(.,aes(Variable,Value,fill=Variable))+ geom_col(position='dodge')) but I can't figure out how to get the SplitVar values 'A

Reading multiple xlsx files each with multiple sheets - purrr

不羁岁月 提交于 2019-12-11 02:35:34
问题 I have multiple excel files, each with different worksheets. I have tried to use readxl and map to import it to R. However, I was only able to do it using a for loop. The code below works fine but I would like to know if there is a clever way to do this. I keep thinking that I could have done it with map2 but I am missing something. library(tidyverse) library(readxl) library(writexl) ### As a first step, I get all the files from my project folder and create an empty list for looping purposes