purrr

How to reference a column in a nested dataframe (then use purrr::map)

混江龙づ霸主 提交于 2020-06-27 04:14:05
问题 I have a very simple question about referencing data columns within a nested dataframe. For a reproducible example, I'll nest mtcars by the two values of variable am : library(tidyverse) mtcars_nested <- mtcars %>% group_by(am) %>% nest() mtcars_nested which gives data that looks like this. #> # A tibble: 2 x 2 #> # Groups: am [2] #> am data #> <dbl> <list> #> 1 1 <tibble [13 × 10]> #> 2 0 <tibble [19 × 10]> If I now wanted to use purrr::map to take the mean of mpg for each level of am I

Code not working using map from purrr package in R

守給你的承諾、 提交于 2020-06-11 03:10:51
问题 I'm learning the map function in purrr package and have the following code not working: library(purrr) library(dplyr) df1 = data.frame(type1 = c(rep('a',5),rep('b',5)), x = 1:10, y = 11:20) df1 %>% group_by(type1) %>% nest() %>% map(.$data,with(.x, x + y)) df1 %>% group_by(type1) %>% nest() %>% map(.$data,function(df) df$x + df$y) For the last two block of code, the errors return as: Error: Index 1 must have length 1 By contrary, the following two blocks of code work well, df1 %>% group_by

Code not working using map from purrr package in R

倖福魔咒の 提交于 2020-06-11 03:10:22
问题 I'm learning the map function in purrr package and have the following code not working: library(purrr) library(dplyr) df1 = data.frame(type1 = c(rep('a',5),rep('b',5)), x = 1:10, y = 11:20) df1 %>% group_by(type1) %>% nest() %>% map(.$data,with(.x, x + y)) df1 %>% group_by(type1) %>% nest() %>% map(.$data,function(df) df$x + df$y) For the last two block of code, the errors return as: Error: Index 1 must have length 1 By contrary, the following two blocks of code work well, df1 %>% group_by

purrr + dplyr NSE issues inside a user written function

泪湿孤枕 提交于 2020-06-09 19:17:40
问题 After a lot of trial and error and consultation with previous answers such as How to detect if bare variable or string I think I have gotten most of what I need done myself. But I'm eager to understand if I'm making some bad assumptions or approaching the problem foolishly before I carry my "solution" into production. Consider the following data: library(dplyr) library(purrr) library(tidyselect) set.seed(1111) dat1 <- data.frame(Region = rep(c("r1","r2"), each = 100), State = rep(c("NY","MA",

purrr + dplyr NSE issues inside a user written function

自古美人都是妖i 提交于 2020-06-09 19:13:12
问题 After a lot of trial and error and consultation with previous answers such as How to detect if bare variable or string I think I have gotten most of what I need done myself. But I'm eager to understand if I'm making some bad assumptions or approaching the problem foolishly before I carry my "solution" into production. Consider the following data: library(dplyr) library(purrr) library(tidyselect) set.seed(1111) dat1 <- data.frame(Region = rep(c("r1","r2"), each = 100), State = rep(c("NY","MA",

How to write a for loop which creates a model and has a function which references that same model

我的梦境 提交于 2020-05-26 09:09:25
问题 I am trying to run a post hoc analysis on an unbalanced two way anova using the anova_test funciton in the rstatix package. I need to run this post hoc test iteratively, as I have ~26 response (y) variables. My first step is to create models of all my y variables with relation to group and treatment . I have successfully managed to do this, creating a single list with 26 models: models <- map(data[,y1:y26], ~(lm(.x ~data$group*data$treatment))) Now comes the part I'm stuck on. Referring to

Match strings before special character

房东的猫 提交于 2020-05-16 02:21:28
问题 I am trying to match strings in two columns and return mismatches before ":". It should not return if x2x, y67y, as x remains x and y remains as y. I don't want to match the ":decimal". If x2y is in both columns then its a match (irrespective of the mismatch in the decimal after special character) INPUT: input <- structure(list(x = structure(c(1L, 2L, 3L, 3L), .Label = c("A", "B", "C"), class = "factor"), y = structure(c(2L, 3L, 1L, 4L), .Label = c("A", "B", "C", "D"), class = "factor"), x

Error using dplyr::count() within purrr::map()

徘徊边缘 提交于 2020-05-13 09:42:08
问题 In this example I want to apply the count() function to every character variable in a dataset. library(dplyr) library(purrr) nycflights13::flights %>% select_if(is.character) %>% map(., count) But I receive the error message: Error in UseMethod("groups") : no applicable method for 'groups' applied to an object of class "character" I'm not sure how to interpret the error message or update my code. Similar code works for numeric variables, but factor variables produce a similar error message to

loess regression on each group with dplyr::group_by()

梦想的初衷 提交于 2020-05-07 05:53:58
问题 Alright, I'm waving my white flag. I'm trying to compute a loess regression on my dataset. I want loess to compute a different set of points that plots as a smooth line for each group. The problem is that the loess calculation is escaping the dplyr::group_by function, so the loess regression is calculated on the whole dataset. Internet searching leads me to believe this is because dplyr::group_by wasn't meant to work this way. I just can't figure out how to make this work on a per-group basis

loess regression on each group with dplyr::group_by()

邮差的信 提交于 2020-05-07 05:50:48
问题 Alright, I'm waving my white flag. I'm trying to compute a loess regression on my dataset. I want loess to compute a different set of points that plots as a smooth line for each group. The problem is that the loess calculation is escaping the dplyr::group_by function, so the loess regression is calculated on the whole dataset. Internet searching leads me to believe this is because dplyr::group_by wasn't meant to work this way. I just can't figure out how to make this work on a per-group basis