purrr

How to get list name and slice name with pipe and purrr

孤者浪人 提交于 2019-12-05 08:03:59
I wonder how to get the list name or group name as a flag when using pipe operation with purrr. for example: I want to use a dynameic parameter of each list name pass to the ggsave function. require(purrr) require(ggplot2) lst=list(a1=data.frame(x=1:10,y=2:11),a2=data.frame(x=1:10,y=-1*2:11)) df=rbind(transform(lst[[1]],id="a1"),transform(lst[[2]],id="a2")) lst %>% map(~ggsave(plot=qplot(data=.,x="x",y="y",geom="line"),file=paste(listname(.),".png"))) df %>% slice_rows("id") %>% by_slice(~ggsave(plot=qplot(data=.,x="x",y="y",geom="line"),file=paste("slicename(.)",".png"))) the slicename(.)

advice on Usage of dplyr:: do vs purrr: map, tidy::nest, for predictions

我的梦境 提交于 2019-12-05 06:44:59
问题 I just came across the the purrr package and I think this would help me out a bit in terms of what I want to do - I just can't put it together. I think this is going to be along post but goes over a common use case I think many others run into so hopefully this is of use to them as well. This is what I'm aiming for: From one big dataset run multiple models on each of the different subgroups. Have these models readily available so I can examine - for coeffients, accuracy, etc. From this saved

Summing Multiple Groups of Columns

本秂侑毒 提交于 2019-12-05 02:23:07
I have a situation where my data frame contains the results of image analysis where the columns are the proportion of a particular class present in the image, such that an example dataframe class_df would look like: id A B C D E F 1 0.20 0.30 0.10 0.15 0.25 0.00 2 0.05 0.10 0.05 0.30 0.10 0.40 3 0.10 0.10 0.10 0.20 0.20 0.30 Each of these classes belongs to a functional group and I want to create new columns where the proportions of each functional group are calculated from the classes. An example mapping class_fg class fg A Z B Z C Z D Y E Y F X and the desired result would be (line added to

Handling vectors of different lengths in purrr

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 02:08:50
问题 I currently have the following R code that runs multiple regression models with different predictors, across different subsets, and returns tidied output using the broom package. library(dplyr) library(purrr) library(broom) cars <- mtcars preds<-c("disp", "drat", "wt") model_fits <- map_df(preds, function(pred) { model_formula <- sprintf("mpg ~ %s", pred) cars %>% group_by(cyl) %>% do(tidy(lm(model_formula, data = .), conf.int = T)) %>% filter(term == pred) %>% mutate(outcome = "mpg") %>%

bunch recoding of variables in the tidyverse (functional / meta-programing)

会有一股神秘感。 提交于 2019-12-04 19:24:54
I want to recode a bunch of variables with as few function calls as possible. I have one data.frame where I want to recode a number of variables. I create a named list of all variable names and the recoding arguments I want to execute. Here I have no problem using map and dpylr . However, when it comes to recoding I find it much easier using recode from the car package, instead of dpylr 's own recoding function. A side question is whether there is a nice way of doing the same thing with dplyr::recode . As a next step I break the data.frame down into a nested tibble. Here I want to do specific

Error in bind_rows_(x, .id) : Argument 1 must have names using map_df in purrr

笑着哭i 提交于 2019-12-04 17:47:59
问题 I'm using the spotifyr package to scrape spotify audio features for every song of specific albums in my dataset. My issue is that my dataset consists of some artists that are not on spotify -- so they shouldn't be returning any values. My issue is that when I get to an artist that is not on spotify, I get this error: Error in bind_rows_(x, .id) : Argument 1 must have names I've tried wrapping the function in tryCatch to get NA for each column of the problematic row, but it doesn't seem to

R: dplyr solution for for-loop with initial conditions set

别来无恙 提交于 2019-12-04 14:27:22
问题 I have a data which has 40 days of the year and some data set.seed(123) df <- data.frame(day = 1:40,rain = runif(40,min = 0, max = 3), petc = runif(40, min = 0.3, max = 8),swc = runif(40, min = 27.01, max = 117.43)) I want to calculate another variable called aetc for each day which is calculated as follows: SW.ini <- 2 # setting some initial values SW.max <- 5 SW.min <- 0 For day 1, 1) Determine a variable called PAW(day1) = SW.ini + rain(day1) 2) If PAW(day1) >= SWC(day1), aetc(day1) = petc

Updating a linear regression model with update and purrr

牧云@^-^@ 提交于 2019-12-04 13:46:51
I want to update a lm -model using the update -function inside a map -call, but this throws the following error: mtcars %>% group_by(cyl) %>% nest() %>% mutate(lm1 = map(data, ~lm(mpg ~ wt, data = .x)), lm2 = map(lm1, ~update(object = .x, formula = .~ . + hp))) Error in mutate_impl(.data, dots) : Evaluation error: cannot coerce class ""lm"" to a data.frame. Can anyone help me with this problem? I am confused about this error, because e.g. this works totally fine: mtcars %>% group_by(cyl) %>% nest() %>% mutate(lm1 = map(data, ~lm(mpg ~ wt, data = .x)), lm2 = map_dbl(lm1, ~coefficients(.x)[1]))

How to add a column using the mapping vector after purrr::map_df

余生长醉 提交于 2019-12-04 10:25:48
I'm using the mtcars dataset as an example to illustrate my question. I ran linear regression on each cylinder type and put all model result together using map_df. (Code and output below). What I want to do is adding another column named 'cylinder' (4,4,6,6,8,8). How can I do that in map_df? When I add argument .id='cylinder', I only got a column as 1,1,2,2,3,3. Thanks a lot in advance. library(purrr) cyls <- c(4,6,8) map_df(cyls, ~tidy(lm(hp~wt,data=mtcars %>% filter(cyl == .x)))) Using set_names should do it cyls %>% set_names() %>% map_df(., ~tidy(lm(hp~wt,data=mtcars %>% filter(cyl == .x))

Use input of purrr's map function to create a named list as output in R

三世轮回 提交于 2019-12-04 09:07:17
问题 I am using the map function of the purrr package in R which gives as output a list. Now I would like the output to be a named list based on the input. An example is given below. input <- c("a", "b", "c") output <- purrr::map(input, function(x) {paste0("test-", x)}) From this I would like to access elements of the list using: output$a Or output$b 回答1: We just need to name the list names(output) <- input and then extract the elements based on the name output$a #[1] "test-a" If this needs to be