purrr

Estimating multiple `lm` models and returning output in one table, with map()

ぃ、小莉子 提交于 2020-01-24 12:32:55
问题 I need to estimate a number of linear models on the same dataset, and put the regression results all into one table. For a reproducible example, here's a simplification using mtcars : formula_1 = "mpg ~ disp" formula_2 = "mpg ~ log(disp)" formula_3 = "mpg ~ disp + hp" Currently, my approach has been to: Create a list that contains all of the formulae. use purrr:map() to estimate all of the lm models. use stargazer:: to produce output tables. library(tidyverse) library(stargazer) formula_1 =

unable to plot kaplan-meier curve with survfit object from a list using ggsurvplot

浪尽此生 提交于 2020-01-23 08:20:26
问题 I am trying to plot Kaplan-Meyer curve using ggsurvplot from survminer package. I'm unable to plot it when I pass a survfit object saved in a list. Let me use lung dataset as a example. Everything works below: library("survival") library("survminer") fit <- survfit(Surv(time, status) ~ sex, data = lung) ggsurvplot(fit, conf.int = TRUE, risk.table.col = "strata", palette = c("#E7B800", "#2E9FDF"), xlim = c(0, 600)) Now I do survfit on two variables and save the model result in a list. Then

Recursively ensuring tibbles instead of data frames when parsing/manipulating nested JSON

試著忘記壹切 提交于 2020-01-21 14:02:26
问题 I have to deal with JSON documents that contain nested documents and at some level have an array which in turn contains individual documents that conceptionally would map back to "data frame rows" when reading/parsing the JSON in R. First order problem/question I'm looking for a way to ensure that either all data frames are always turned into tibbles or that at least the "leaf data frames" become tibbles while the the "parent data frames" are allowed to become lists for arbitrary nested

Recursively ensuring tibbles instead of data frames when parsing/manipulating nested JSON

血红的双手。 提交于 2020-01-21 14:02:05
问题 I have to deal with JSON documents that contain nested documents and at some level have an array which in turn contains individual documents that conceptionally would map back to "data frame rows" when reading/parsing the JSON in R. First order problem/question I'm looking for a way to ensure that either all data frames are always turned into tibbles or that at least the "leaf data frames" become tibbles while the the "parent data frames" are allowed to become lists for arbitrary nested

Use functions and parameters stored in list-cols - Purrr

懵懂的女人 提交于 2020-01-17 07:06:34
问题 I have the following tibble: # A tibble: 18 × 6 id columnFilter modelName model train.X train.Y <int> <chr> <chr> <list> <list> <list> 1 1 groupedColumns.donr boostModel <fun> <tibble [3,984 × 17]> <fctr [3,984]> 2 2 groupedSquaredColumns.donr boostModel <fun> <tibble [3,984 × 28]> <fctr [3,984]> 3 3 groupedTransformedColumns.donr boostModel <fun> <tibble [3,984 × 17]> <fctr [3,984]> 4 4 ungroupedColumns.donr boostModel <fun> <tibble [3,984 × 17]> <fctr [3,984]> 5 5 ungroupedSquaredColumns

Use functions and parameters stored in list-cols - Purrr

£可爱£侵袭症+ 提交于 2020-01-17 07:06:05
问题 I have the following tibble: # A tibble: 18 × 6 id columnFilter modelName model train.X train.Y <int> <chr> <chr> <list> <list> <list> 1 1 groupedColumns.donr boostModel <fun> <tibble [3,984 × 17]> <fctr [3,984]> 2 2 groupedSquaredColumns.donr boostModel <fun> <tibble [3,984 × 28]> <fctr [3,984]> 3 3 groupedTransformedColumns.donr boostModel <fun> <tibble [3,984 × 17]> <fctr [3,984]> 4 4 ungroupedColumns.donr boostModel <fun> <tibble [3,984 × 17]> <fctr [3,984]> 5 5 ungroupedSquaredColumns

Adding grouped variable name to plot with group_nest and map

妖精的绣舞 提交于 2020-01-16 09:40:34
问题 This is likely a syntax question. Here's the problem recreated with some dummy data. I'm working with a larger dataset, and I want to perform a group_nest and then create some plots with the title of the group. All goes well, until the title is just repeated. library(tidyverse) N <- 30 df <- tibble(type = rep(c("small","medium","high"), each=N/3), dummy = rep(c(1,5,10),each=10), xvals = rep(1:10,3), A = rnorm(N)*dummy, B = rnorm(N)*dummy, C = rnorm(N)*dummy) %>% mutate(type = factor(type,

Adding grouped variable name to plot with group_nest and map

左心房为你撑大大i 提交于 2020-01-16 09:40:11
问题 This is likely a syntax question. Here's the problem recreated with some dummy data. I'm working with a larger dataset, and I want to perform a group_nest and then create some plots with the title of the group. All goes well, until the title is just repeated. library(tidyverse) N <- 30 df <- tibble(type = rep(c("small","medium","high"), each=N/3), dummy = rep(c(1,5,10),each=10), xvals = rep(1:10,3), A = rnorm(N)*dummy, B = rnorm(N)*dummy, C = rnorm(N)*dummy) %>% mutate(type = factor(type,

using purrr to extract elements from multiple lists starting with a common letter

岁酱吖の 提交于 2020-01-16 05:42:07
问题 I have a list of lists. One element in each list has a name beginning with "n_". How do I extract these elements and store them in a separate list? Can I use a combination of map and starts_with ? E.g.: m1 <- list(n_age = c(19,40,39), names = c("a", "b", "c")) m2 <- list(n_gender = c("m","f","f"), names = c("f", "t", "d")) nice_list <- list(m1, m2) I was hoping that something like the following to work (it doesn't!): output <- map(nice_list, starts_with("n_")) 回答1: How about this? map(nice

R purrr map show column names in output

拜拜、爱过 提交于 2020-01-15 10:13:25
问题 I'm trying to run purrr map across vector inputs, and in the output i'd like the output columns to have meaningful names. x <- c("a", "b") y <- "end." map_dfc(x, function(x) paste("pre ", x, y)) names(x) <- x map_dfc(x, function(x) paste(x, y)) This is the output I expect, which has column names a and b : # A tibble: 1 x 2 # a b # <chr> <chr> # pre a end. pre b end. Is there a way to avoid the need to run names(x) <- x i.e. x <- c("a", "b") y <- "end." map_dfc(x, function(x) paste("pre ", x,