purrr

How to use purrr's map function to perform row-wise prop.tests and add results to the dataframe?

喜欢而已 提交于 2019-11-29 23:50:03
问题 I'm trying to solve the following problem in R: I have a dataframe with two variables (number of successes, and number of total trials). # A tibble: 4 x 2 Success N <dbl> <dbl> 1 28. 40. 2 12. 40. 3 22. 40. 4 8. 40. I would like to perform a prop.test or binom.test on each row and add the resulting list to the dataframe (or certain elements of it, like the p-value and CIs). Ideally, I would like to add a third column with the p-values and the CI-range. My attempts so far were painly

Add titles to ggplots created with map()

时间秒杀一切 提交于 2019-11-29 21:13:59
问题 What's the easiest way to add titles to each ggplot that I've created below using the map function? I want the titles to reflect the name of each data frame - i.e. 4, 6, 8 (cylinders). Thanks :) mtcars_split <- mtcars %>% split(mtcars$cyl) plots <- mtcars_split %>% map(~ ggplot(data=.,mapping = aes(y=mpg,x=wt)) + geom_jitter() # + ggtitle(....)) plots 回答1: Use map2 with names . plots <- map2( mtcars_split, names(mtcars_split), ~ggplot(data = .x, mapping = aes(y = mpg, x = wt)) + geom_jitter()

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

狂风中的少年 提交于 2019-11-29 14:20:10
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 vectors are same, but please assume they are different and retain them as different vectors. Now I want to

Join tables based on multiple ranges in R

扶醉桌前 提交于 2019-11-29 12:14:17
I have a situation where I have two data frames I would like to join. The table params describes the param for a unit in terms time and angle ranges. The table data is longer and contains id, time and angle parameters. I would like to join the param value from params when id match and time is in the range between valid_from and valid_to and ang is between angle_begin angle_end in the data table. Below is an example of the tables. params <- data.frame(id = 1:4 ,valid_from = 1 ,valid_to = c(10, 20, 30, 40) ,angle_begin = c(120, 90, 0, 50) ,angle_end = c(180, 170, 160, 150) ,param = c("A", "B",

rolling regression by group in the tidyverse?

孤者浪人 提交于 2019-11-29 10:04:55
There are many questions about rolling regression in R, but here I am specifically looking for something that uses dplyr , broom and (if needed) purrr . This is what makes this question different. I want to be tidyverse consistent. Is is possible to do a proper running regression with tidy tools such as purrr:map and dplyr ? Please consider this simple example: library(dplyr) library(purrr) library(broom) library(zoo) library(lubridate) mydata = data_frame('group' = c('a','a', 'a','a','b', 'b', 'b', 'b'), 'y' = c(1,2,3,4,2,3,4,5), 'x' = c(2,4,6,8,6,9,12,15), 'date' = c(ymd('2016-06-01', '2016

Add new variable to list of data frames with purrr and mutate() from dplyr

别等时光非礼了梦想. 提交于 2019-11-29 00:05:08
I know that there are many related questions here on SO, but I am looking for a purrr solution, please, not one from the apply list of functions or cbind/rbdind (I want to take this opportunity to get to know purrr better). I have a list of dataframes and I would like to add a new column to each dataframe in the list. The value of the column will be the name of the dataframe, i.e. the name of each element in the list. There is something similar here , but it involves the use of a function and mutate_each() , whereas I need just mutate() . To give you an idea of the list (called comentarios ),

How to use map from purrr with dplyr::mutate to create multiple new columns based on column pairs

孤者浪人 提交于 2019-11-28 07:02:15
I have to following issue using R. In short I want to create multiple new columns in a data frame based on calculations of different column pairs in the data frame. The data looks as follows: df <- data.frame(a1 = c(1:5), b1 = c(4:8), c1 = c(10:14), a2 = c(9:13), b2 = c(3:7), c2 = c(15:19)) df a1 b1 c1 a2 b2 c2 1 4 10 9 3 15 2 5 11 10 4 16 3 6 12 11 5 17 4 7 13 12 6 18 5 8 14 13 7 19 The output is supposed to look like the following: a1 b1 c1 a2 b2 c2 sum_a sum_b sum_c 1 4 10 9 3 15 10 7 25 2 5 11 10 4 16 12 9 27 4 7 13 12 6 18 16 13 31 5 8 14 13 7 19 18 15 33 I can achieve this using dplyr

Join tables based on multiple ranges in R

喜你入骨 提交于 2019-11-28 05:54:07
问题 I have a situation where I have two data frames I would like to join. The table params describes the param for a unit in terms time and angle ranges. The table data is longer and contains id, time and angle parameters. I would like to join the param value from params when id match and time is in the range between valid_from and valid_to and ang is between angle_begin angle_end in the data table. Below is an example of the tables. params <- data.frame(id = 1:4 ,valid_from = 1 ,valid_to = c(10,

rolling regression by group in the tidyverse?

三世轮回 提交于 2019-11-28 00:19:50
问题 There are many questions about rolling regression in R, but here I am specifically looking for something that uses dplyr , broom and (if needed) purrr . This is what makes this question different. I want to be tidyverse consistent. Is is possible to do a proper running regression with tidy tools such as purrr:map and dplyr ? Please consider this simple example: library(dplyr) library(purrr) library(broom) library(zoo) library(lubridate) mydata = data_frame('group' = c('a','a', 'a','a','b', 'b

using lm in list column to predict new values using purrr

浪子不回头ぞ 提交于 2019-11-27 23:23:28
I am trying to add a column of predictions to a dataframe that has a list column that contains an lm model. I adopted some of the code from this post . I have made a toy example here: library(dplyr) library(purrr) library(tidyr) library(broom) set.seed(1234) exampleTable <- data.frame( ind = c(rep(1:5, 5)), dep = rnorm(25), groups = rep(LETTERS[1:5], each = 5) ) %>% group_by(groups) %>% nest(.key=the_data) %>% mutate(model = the_data %>% map(~lm(dep ~ ind, data = .))) %>% mutate(Pred = map2(model, the_data, predict)) exampleTable <- exampleTable %>% mutate(ind=row_number()) that gives me a