purrr

How do I pipe into or include an additional argument within a list of quosures?

ぃ、小莉子 提交于 2021-01-27 11:58:00
问题 I have a list of quosures in my_q_list below: library(rlang) suppressPackageStartupMessages(library(dplyr)) q_list <- function(...) { enquos(...) } my_q_list <- q_list( select(mpg, hp), filter(hp > 20), mutate(mpg2 = mpg*2) ) my_q_list #> <list_of<quosure>> #> #> [[1]] #> <quosure> #> expr: ^select(mpg, hp) #> env: global #> #> [[2]] #> <quosure> #> expr: ^filter(hp > 20) #> env: global #> #> [[3]] #> <quosure> #> expr: ^mutate(mpg2 = mpg * 2) #> env: global Created on 2020-07-01 by the

How do I pipe into or include an additional argument within a list of quosures?

喜你入骨 提交于 2021-01-27 11:57:35
问题 I have a list of quosures in my_q_list below: library(rlang) suppressPackageStartupMessages(library(dplyr)) q_list <- function(...) { enquos(...) } my_q_list <- q_list( select(mpg, hp), filter(hp > 20), mutate(mpg2 = mpg*2) ) my_q_list #> <list_of<quosure>> #> #> [[1]] #> <quosure> #> expr: ^select(mpg, hp) #> env: global #> #> [[2]] #> <quosure> #> expr: ^filter(hp > 20) #> env: global #> #> [[3]] #> <quosure> #> expr: ^mutate(mpg2 = mpg * 2) #> env: global Created on 2020-07-01 by the

accessing nested lists in R

强颜欢笑 提交于 2021-01-27 06:24:21
问题 I have created a double nested structure for some data. How can I Access the data on the 2nd Level ( or for that matter the nth Level?) library(gapminder) library(purrr) library(tidyr) gapminder nest_data <- gapminder %>% group_by(continent) %>% nest(.key = by_continent) nest_2<-nest_data %>% mutate(by_continent = map(by_continent, ~.x %>% group_by(country) %>% nest(.key = by_country))) How can I now get the data for China into a dataframe or tibble from nest_2? I can get the data for all of

summarize to vector output

一世执手 提交于 2021-01-27 06:18:06
问题 Let's say I have the following (simplified) tibble containing a group and values in vectors: set.seed(1) (tb_vec <- tibble(group = factor(rep(c("A","B"), c(2,3))), values = replicate(5, sample(3), simplify = FALSE))) # A tibble: 5 x 2 group values <fct> <list> 1 A <int [3]> 2 A <int [3]> 3 B <int [3]> 4 B <int [3]> 5 B <int [3]> tb_vec[[1,2]] [1] 1 3 2 I would like to summarize the values vectors per group by summing them (vectorized) and tried the following: tb_vec %>% group_by(group) %>%

How would I write a recursive version of purrr::keep?

走远了吗. 提交于 2021-01-27 05:54:10
问题 Say I have a nested list with a bunch of data frames at different levels. I want to extract out flattened list of just the data frames. How might I write this using purrr functions? Should I be looking at reduce ? For example, given the data: s <- list(x = 1:10, data = data.frame(report = LETTERS[1:5], value = rnorm(5, 20, 5)), report = list(A = data.frame(x = 1:3, y = c(2, 4, 6)), B = data.frame(x = 1:3, y = c(3, 6, 9)), z = 4:10, other = data.frame(w = 3:5, color = c("red", "green", "blue")

How would I write a recursive version of purrr::keep?

*爱你&永不变心* 提交于 2021-01-27 05:53:28
问题 Say I have a nested list with a bunch of data frames at different levels. I want to extract out flattened list of just the data frames. How might I write this using purrr functions? Should I be looking at reduce ? For example, given the data: s <- list(x = 1:10, data = data.frame(report = LETTERS[1:5], value = rnorm(5, 20, 5)), report = list(A = data.frame(x = 1:3, y = c(2, 4, 6)), B = data.frame(x = 1:3, y = c(3, 6, 9)), z = 4:10, other = data.frame(w = 3:5, color = c("red", "green", "blue")

can't use emmeans inside map

£可爱£侵袭症+ 提交于 2021-01-24 08:12:29
问题 This works: testmodel=glm(breaks~wool,data=warpbreaks) emmeans::emmeans(testmodel,"wool") This works: warpbreaks %>% group_by(tension) %>% do(models=glm(breaks~wool,data=.)) %>% ungroup() %>% mutate(means=map(models,~emmeans::emmeans(.x,"wool"))) This doesn't: warpbreaks %>% group_by(tension) %>% nest() %>% mutate(models=map(data,~glm(breaks~wool,data=.x))) %>% mutate(means=map(models,~emmeans::emmeans(.x,"wool"))) Error in is.data.frame(data) : object '.x' not found Error in mutate_impl(

Modifying specified list elements r

雨燕双飞 提交于 2021-01-24 07:48:29
问题 I have an output that looks like this: test_list <- list(list(x = c(10, 101, 3), y = c(9, 12, 11)), list(x = c(10, 133, 4), y = c(9, 15, 13)), list(x = c(10, 101, 90), y = c(9, 18, 11)), list(x = c(10, 101, 1), y = c(9, 10, 15))) I would like to select and modify specific elements of sublists x and y. For example I would like to replace the last element of x with a number from a vector or generated from a function - i.e; map the vector z <- c(10, 50, 6, 12) over the last element of sublists x

Modifying specified list elements r

寵の児 提交于 2021-01-24 07:48:12
问题 I have an output that looks like this: test_list <- list(list(x = c(10, 101, 3), y = c(9, 12, 11)), list(x = c(10, 133, 4), y = c(9, 15, 13)), list(x = c(10, 101, 90), y = c(9, 18, 11)), list(x = c(10, 101, 1), y = c(9, 10, 15))) I would like to select and modify specific elements of sublists x and y. For example I would like to replace the last element of x with a number from a vector or generated from a function - i.e; map the vector z <- c(10, 50, 6, 12) over the last element of sublists x

`group_by` and keep grouping levels as nested data frame's name

痞子三分冷 提交于 2021-01-07 06:57:45
问题 This question is related to and inspired by can't use emmeans inside map I am doing several steps of data analysis with the following code. I want to keep my grouping factor's levels as the nested data frames' names and uses those names to identify each of the steps along the way, instead of using the default enumeration [[1]], [[2]], [[3]], etc. I don't understand the error I got. Please see how I can fix my code. library(dplyr) library(purrr) library(emmeans) data("warpbreaks") wb_emm <-