purrr

propagating controls for a number of nested groups

情到浓时终转凉″ 提交于 2021-01-07 03:03:30
问题 This is a follow-up question to a previous post here. I got a good set of answers from @akrun for my toy problem, but when going through the answer I realized that it is really not yet applicable to the real-life problem. This illustration of the problem is still correct: : Before: After: The further challenge is that the 'grp' and 'treatment' variables are just the levels I am describing because these are where the controls need to be propagated. In fact there are four additional grouping

Map `joint_tests` to a list after fitting a `gls` model

二次信任 提交于 2021-01-07 02:27:16
问题 I am trying to get the type 3 ANOVA table with emmeans::joint_tests() from a list with the following code. I don't fully understand the error message. The code that tutors me came from http://pages.stat.wisc.edu/~yandell/R_for_data_sciences/curate/tidyverse.html library(dplyr) library(nlme) library(emmeans) data("diamonds") diamonds %>% split(.$cut) %>% map(~ gls(price ~ x + y + z, weights = varIdent(form = ~ 1|color), data = .))%>% map(summary) The error message seems to suggest that I save

Nested list to dataframe [using purrr + map]

别来无恙 提交于 2021-01-01 09:11:40
问题 I've looked at a lot of posts so I'm sorry if this is redundant, but was hoping to get some help flattening a nested list: test <- list() test <- c( list("A" = c(list("1"), list("2"), list("3"))), list("B" = c(list("4"), list("5"), list("6"))) ) Desired Output name subcat 1 A 1 2 A 2 3 A 3 4 B 4 5 B 5 6 B 6 I'm struggling to write a nested for loop but I'd really like to use purrr or something more elegant to create a dataframe with two columns: the subcat column, and a repeated column for

Looping over unique pairs of elements in a list in R

大憨熊 提交于 2020-12-30 04:44:31
问题 Suppose I have a list of objects called bb . I want to pick each unique pair of elements in bb and use them in some kind of function (called convolve ) as shown below: ## Below `bb` elements: `bma1` & `bma2` are used in the function: con <- convolve(dens1= bb$bma1$dposterior, dens2= function(x){bb$bma2$dposterior(-x)}, cdf1= bb$bma1$pposterior, cdf2= function(x){1 - bb$bma2$pposterior(-x)}) con$quantile(c(0.025, 0.975)) Question: bb can have any number of elements but convolve() accepts only

How to optimize a function that matches observations according to certain criteria

狂风中的少年 提交于 2020-12-30 04:20:30
问题 I am looking for a more efficient way of doing an operation with a given dataframe. library(purrr) library(dplyr) Here is a step by step description: First, there is the function possible_matches , that for each observation i in df , gives the index of rows that are possibly matchable to i , which are going to be used on the next step: possible_matches <- function(i, df) { k1 <- df$j[df$id_0 == df$id_0[i]] j2 <- setdiff(df$j, k1) k2 <- map(j2, ~ df$j[df$id_0[.] == df$id_0]) k3 <- map(k2, ~

How to optimize a function that matches observations according to certain criteria

泪湿孤枕 提交于 2020-12-30 04:09:55
问题 I am looking for a more efficient way of doing an operation with a given dataframe. library(purrr) library(dplyr) Here is a step by step description: First, there is the function possible_matches , that for each observation i in df , gives the index of rows that are possibly matchable to i , which are going to be used on the next step: possible_matches <- function(i, df) { k1 <- df$j[df$id_0 == df$id_0[i]] j2 <- setdiff(df$j, k1) k2 <- map(j2, ~ df$j[df$id_0[.] == df$id_0]) k3 <- map(k2, ~

How to optimize a function that matches observations according to certain criteria

帅比萌擦擦* 提交于 2020-12-30 04:09:41
问题 I am looking for a more efficient way of doing an operation with a given dataframe. library(purrr) library(dplyr) Here is a step by step description: First, there is the function possible_matches , that for each observation i in df , gives the index of rows that are possibly matchable to i , which are going to be used on the next step: possible_matches <- function(i, df) { k1 <- df$j[df$id_0 == df$id_0[i]] j2 <- setdiff(df$j, k1) k2 <- map(j2, ~ df$j[df$id_0[.] == df$id_0]) k3 <- map(k2, ~

How to optimize a function that matches observations according to certain criteria

痞子三分冷 提交于 2020-12-30 04:08:31
问题 I am looking for a more efficient way of doing an operation with a given dataframe. library(purrr) library(dplyr) Here is a step by step description: First, there is the function possible_matches , that for each observation i in df , gives the index of rows that are possibly matchable to i , which are going to be used on the next step: possible_matches <- function(i, df) { k1 <- df$j[df$id_0 == df$id_0[i]] j2 <- setdiff(df$j, k1) k2 <- map(j2, ~ df$j[df$id_0[.] == df$id_0]) k3 <- map(k2, ~

R (purrr) flatten list of named lists to list and keep names

妖精的绣舞 提交于 2020-12-26 07:39:27
问题 Maybe I'm missing something obvious but trying to flatten a list of named lists of named lists in R (may even be more nested) into eventually one flat list. purrr and rlist seem to have tools for that. How can I achieve that names of sublists become name precrypts of flattened result list, e.g. list1.blist.a in purrr ? My actual list is more deeply nested with varying number of levels and repeating names on different levels. In the end I perform purrr::map_df(final_list, bind_rows) and this

Programmatically create tab and plot in markdown

非 Y 不嫁゛ 提交于 2020-12-23 11:10:13
问题 I'm trying to create a dynamic number of tabs in my rmd with some content inside. This one doesn't help. Something like this: --- title: "1" output: html_document --- ```{r } library(highcharter) library(tidyverse) iris %>% dplyr::group_split(Species) %>% purrr::map(.,~{ # create tabset for each group ..1 %>% hchart("scatter", hcaes(x = Sepal.Length, y = Sepal.Width)) }) ``` 回答1: You can set results = 'asis' knitr option to generate the tabs in the map function using cat . Getting Highcharter