Reputation: 7517
I understand the lifecycle of do()
from dplyr
has now been superseded.
However, in my example below, what can I use instead of do()
to reproduce the exact same result that do()
gives?
library(tidyverse)
library(broom)
AWD <- read.csv('https://raw.githubusercontent.com/rnorouzian/e/master/aw.csv')
AWD_long <- AWD %>%
gather(key = "var", value = "Outcome", -Patient) %>%
separate(var, into = c("Outcome_Measure", "Treatment"), sep = " - ") %>%
select(Patient, Treatment, Outcome_Measure, Outcome)
AWD_long %>%
group_by(Outcome_Measure) %>%
do(tidy(t.test(Outcome ~ Treatment, paired = TRUE, data = .))) # what can I use to replace `do()` here?
Upvotes: 1
Views: 38
Reputation: 887088
Here is an option with nest_by
library(dplyr)
library(tidyr)
library(broom)
AWD_long %>%
nest_by(Outcome_Measure) %>%
transmute(new = list(tidy(t.test(Outcome ~ Treatment,
paired = TRUE, data = data)))) %>%
unnest(c(new))
-output
# A tibble: 3 x 9
# Groups: Outcome_Measure [3]
# Outcome_Measure estimate statistic p.value parameter conf.low conf.high method alternative
# <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
#1 Direct cost 6976. 4.70 0.00221 7 3464. 10487. Paired t-test two.sided
#2 ICU days 1.38 1.95 0.0923 7 -0.293 3.04 Paired t-test two.sided
#3 Length of stay (Hours) 108 5.32 0.00109 7 60.0 156. Paired t-test two.sided
Or another option is map
after doing a group_split
library(purrr)
AWD_long %>%
group_split(Outcome_Measure) %>%
map_dfr(~ tidy(t.test(Outcome ~ Treatment, paired = TRUE, data = .x)) %>%
bind_cols(.x['Outcome_Measure'] %>%
slice(1), .))
Upvotes: 1