Reputation: 21
I would like to perform pairwise.t.test in the nested data frame. Is it possible?
library(dplyr)
library(broom)
library(tidyr)
library(purrr)
a <- mtcars%>%
dplyr::select(disp, gear,am)%>%
nest(-am)%>%
mutate(t_test = map(data, ~ pairwise.t.test(disp, gear, p.adjust = "bonferroni")))
# Error in factor(g) : object 'gear' not found
Upvotes: 1
Views: 1276
Reputation: 1563
You have to specify the data frames inside the nested column using .x
.
a <- mtcars %>%
select(disp, gear,am) %>%
nest(data = c(-am)) %>%
mutate(t_test = map(data, ~pairwise.t.test(.x$disp, .x$gear, p.adjust = "bonferroni")))
If you don't need the (nested) column data
you can do it with just group_by
and summarise
a <- mtcars %>%
select(disp, gear,am) %>%
group_by(am) %>%
summarise(t_test = list(pairwise.t.test(disp, gear, p.adjust = "bonferroni")))
In both cases, you can get the results into a table form using broom
(i guess you wanted that since you load that package).
a %>%
mutate(t_test = map(t_test, broom::tidy)) %>%
unnest(t_test)
Upvotes: 3
Reputation: 1792
Does this give the output you were looking for?
library(dplyr)
library(broom)
library(tidyr)
library(purrr)
mtcars_sub <- mtcars %>%
dplyr::select(disp, gear, am) %>%
group_by(am) %>%
nest()
t_tests <- function(df){
pairwise.t.test(df$disp, df$gear, p.adjust = "bonferroni")
}
map(mtcars_sub$data, t_tests)
#> [[1]]
#>
#> Pairwise comparisons using t tests with pooled SD
#>
#> data: df$disp and df$gear
#>
#> 4
#> 5 0.048
#>
#> P value adjustment method: bonferroni
#>
#> [[2]]
#>
#> Pairwise comparisons using t tests with pooled SD
#>
#> data: df$disp and df$gear
#>
#> 3
#> 4 0.0027
#>
#> P value adjustment method: bonferroni
Let me know if it's not the right output you need.
Credit to @ronak-shah
https://stackoverflow.com/a/62408521/8742237
Upvotes: 0