I have the following dataset
# dput(head(dat, 130))
dat <- structure(list(
Gender = c("Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women" ),
Education = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Basic", "Medium", "Higher"), class = "factor"),
Efficacy = structure(c(24, 24, 24, 24, 24, 24, 24, 24, 29, 29, 29, 29, 28, 28, 28, 28, 29, 29, 29, 29, 22, 22, 22, 22, 30, 30, 30, 30, 26, 26, 26, 26, 16, 16, 16, 16, 22, 22, 22, 22, 26, 26, 26, 26, 30, 30, 30, 30, 32, 32, 32, 32, 20, 20, 20, 20, 24, 24, 24, 24, 31, 31, 31, 31, 28, 28, 28, 28, 26, 26, 26, 26, 25, 25, 25, 25, 35, 35, 35, 35, 30, 30, 30, 30, 25, 25, 25, 25, 28, 28, 28, 28, 33, 33, 33, 33, 33, 33, 33, 33, 22, 22, 22, 22, 24, 24, 24, 24, 33, 33, 33, 33, 27, 27, 27, 27, 35, 35, 35, 35, 26, 26, 26, 26, 22, 22, 22, 22, 25, 25 ), format.spss = "F16.2"),
grp = c("Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring"),
points = c(9, 20, 12, 15, 10, 22, 8, 14, 12, 25, 11, 17, 10, 21, 12, 18, 13, 22, 12, 16, 11, 11, 8, 11, 10, 21, 14, 20, 8, 19, 5, 18, 5, 15, 7, 11, 6, 18, 12, 16, 7, 23, 10, 19, 8, 24, 15, 22, 12, 19, 10, 20, 6, 20, 7, 14, 9, 20, 7, 15, 10, 22, 7, 21, 9, 19, 13, 19, 11, 24, 7, 15, 9, 22, 9, 16, 13, 22, 12, 22, 11, 24, 13, 19, 10, 19, 11, 15, 10, 19, 11, 18, 14, 25, 15, 19, 13, 25, 7, 20, 9, 22, 5, 13, 8, 19, 5, 16, 11, 16, 10, 22, 7, 24, 12, 20, 13, 24, 13, 22, 6, 22, 4, 20, 8, 17, 8, 14, 10, 19)), row.names = c(NA, -130L), class = c("tbl_df", "tbl", "data.frame")
)
where I should extract some statistics from. I am applying the map
function to get them. I have used the following code as a sample, that seems to work fine but not so precisely:
dat %>%
nest(data = -c(grp)) %>%
mutate(test = map(data, ~ tidy(t.test(points ~ Gender, alternative = 'greater', .x))),
sd = map(data, ~.x %>% group_by(Gender) %>% summarise(sd = sd(.data$points)))) %>%
unnest(test, sd)
because I would like to fix the reference group (Women) and use them as alternative arguments for each string in the order reported in the hyp vector, one for each grp
level:
hyp = c(rep('greater',2), 'less', 'greater')
I opted to choose the t_test function, but aside to do not knowing what put in the argument alternative
library(rstatix)
dat %>%
nest(data = -c(grp)) %>%
mutate(test = map(data, ~ t_test(points ~ Gender, ref.group = 'Women', .x)),
sd = map(data, ~.x %>% group_by(Gender) %>% summarise(sd = sd(.data$points)))) %>%
unnest(test, sd)
without it turns me the error
Error in `mutate()`:
! Problem while computing `test = map(...)`.
Caused by error in `x$terms %||% attr(x, "terms") %||% stop("no terms component nor attribute")`:
! no terms component nor attribute
Run `rlang::last_error()` to see where the error occurred.
Could someone help me please? Thanks
Your data does not work so I modified the data to provide you a working example. I used pmap()
because it allows more than two inputs to the function:
library(tidyverse, quietly = TRUE)
library(rstatix, quietly = TRUE)
library(magrittr)
#>
dat <- structure(list(
Gender = rep_len(c("Women", "Women", "Women", "Women", "Men", "Men", "Men", "Men"), length.out = 130),
Education = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Basic", "Medium", "Higher"), class = "factor"),
Efficacy = structure(c(24, 24, 24, 24, 24, 24, 24, 24, 29, 29, 29, 29, 28, 28, 28, 28, 29, 29, 29, 29, 22, 22, 22, 22, 30, 30, 30, 30, 26, 26, 26, 26, 16, 16, 16, 16, 22, 22, 22, 22, 26, 26, 26, 26, 30, 30, 30, 30, 32, 32, 32, 32, 20, 20, 20, 20, 24, 24, 24, 24, 31, 31, 31, 31, 28, 28, 28, 28, 26, 26, 26, 26, 25, 25, 25, 25, 35, 35, 35, 35, 30, 30, 30, 30, 25, 25, 25, 25, 28, 28, 28, 28, 33, 33, 33, 33, 33, 33, 33, 33, 22, 22, 22, 22, 24, 24, 24, 24, 33, 33, 33, 33, 27, 27, 27, 27, 35, 35, 35, 35, 26, 26, 26, 26, 22, 22, 22, 22, 25, 25 ), format.spss = "F16.2"),
grp = c("Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring"),
points = c(9, 20, 12, 15, 10, 22, 8, 14, 12, 25, 11, 17, 10, 21, 12, 18, 13, 22, 12, 16, 11, 11, 8, 11, 10, 21, 14, 20, 8, 19, 5, 18, 5, 15, 7, 11, 6, 18, 12, 16, 7, 23, 10, 19, 8, 24, 15, 22, 12, 19, 10, 20, 6, 20, 7, 14, 9, 20, 7, 15, 10, 22, 7, 21, 9, 19, 13, 19, 11, 24, 7, 15, 9, 22, 9, 16, 13, 22, 12, 22, 11, 24, 13, 19, 10, 19, 11, 15, 10, 19, 11, 18, 14, 25, 15, 19, 13, 25, 7, 20, 9, 22, 5, 13, 8, 19, 5, 16, 11, 16, 10, 22, 7, 24, 12, 20, 13, 24, 13, 22, 6, 22, 4, 20, 8, 17, 8, 14, 10, 19)), row.names = c(NA, -130L), class = c("tbl_df", "tbl", "data.frame")
)
dat |>
nest(data = -c(grp)) |>
mutate(alternative = c(rep('greater',2), 'less', 'greater')) %$% # Exposition pipe, allows plucking vectors from data frames https://magrittr.tidyverse.org/reference/exposition.html
pmap_dfr(.l = list(grp = grp, alternative = alternative, dat = data), .f = function(grp = grp, alternative = alternative, dat = dat){
some_other_operation_i_want_to_do_outside_my_pipeline <- dat |> group_by(Gender) |> tally()
dat <- tibble::tibble(
grp = grp,
alternative = alternative,
data = list(dat),
gender_df = list(some_other_operation_i_want_to_do_outside_my_pipeline)
) |>
mutate(
sd = sd(dat$points),
t_test(data = dat, points ~ Gender, ref.group = "Women", alternative = alternative),
)
return(dat)
})
#> # A tibble: 4 × 13
#> grp alter…¹ data gender…² sd .y. group1 group2 n1 n2 stati…³
#> <chr> <chr> <list> <list> <dbl> <chr> <chr> <chr> <int> <int> <dbl>
#> 1 Avoid… greater <tibble> <tibble> 2.32 poin… Women Men 17 16 -0.568
#> 2 Savor… greater <tibble> <tibble> 3.16 poin… Women Men 17 16 0.610
#> 3 Coping less <tibble> <tibble> 3.09 poin… Women Men 16 16 0.113
#> 4 Obtai… greater <tibble> <tibble> 3.16 poin… Women Men 16 16 0.275
#> # … with 2 more variables: df <dbl>, p <dbl>, and abbreviated variable names
#> # ¹alternative, ²gender_df, ³statistic
Created on 2023-03-16 with reprex v2.0.2
t_test()
does not work for your dataset. Referencing another answer from Jouni Helske:
All standard variants of t-test use sample variances in their formulas, and you cannot compute that from one observation as you are dividing with n-1, where n is sample size.
library(tidyverse, quietly = TRUE)
library(rstatix, quietly = TRUE)
#>
dat <- structure(list(
Gender = c("Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women", "Women" ),
Education = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Basic", "Medium", "Higher"), class = "factor"),
Efficacy = structure(c(24, 24, 24, 24, 24, 24, 24, 24, 29, 29, 29, 29, 28, 28, 28, 28, 29, 29, 29, 29, 22, 22, 22, 22, 30, 30, 30, 30, 26, 26, 26, 26, 16, 16, 16, 16, 22, 22, 22, 22, 26, 26, 26, 26, 30, 30, 30, 30, 32, 32, 32, 32, 20, 20, 20, 20, 24, 24, 24, 24, 31, 31, 31, 31, 28, 28, 28, 28, 26, 26, 26, 26, 25, 25, 25, 25, 35, 35, 35, 35, 30, 30, 30, 30, 25, 25, 25, 25, 28, 28, 28, 28, 33, 33, 33, 33, 33, 33, 33, 33, 22, 22, 22, 22, 24, 24, 24, 24, 33, 33, 33, 33, 27, 27, 27, 27, 35, 35, 35, 35, 26, 26, 26, 26, 22, 22, 22, 22, 25, 25 ), format.spss = "F16.2"),
grp = c("Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring"),
points = c(9, 20, 12, 15, 10, 22, 8, 14, 12, 25, 11, 17, 10, 21, 12, 18, 13, 22, 12, 16, 11, 11, 8, 11, 10, 21, 14, 20, 8, 19, 5, 18, 5, 15, 7, 11, 6, 18, 12, 16, 7, 23, 10, 19, 8, 24, 15, 22, 12, 19, 10, 20, 6, 20, 7, 14, 9, 20, 7, 15, 10, 22, 7, 21, 9, 19, 13, 19, 11, 24, 7, 15, 9, 22, 9, 16, 13, 22, 12, 22, 11, 24, 13, 19, 10, 19, 11, 15, 10, 19, 11, 18, 14, 25, 15, 19, 13, 25, 7, 20, 9, 22, 5, 13, 8, 19, 5, 16, 11, 16, 10, 22, 7, 24, 12, 20, 13, 24, 13, 22, 6, 22, 4, 20, 8, 17, 8, 14, 10, 19)), row.names = c(NA, -130L), class = c("tbl_df", "tbl", "data.frame")
)
t_test(dat, points ~ Gender, ref.group = "Women", alternative = "less")
#> Error in t.test.default(x = c(9, 20, 12, 15, 10, 22, 8, 14, 12, 25, 11, : not enough 'y' observations
Created on 2023-03-16 with reprex v2.0.2
Adding another Gender to the data set will resolve that issue:
library(tidyverse, quietly = TRUE)
library(rstatix, quietly = TRUE)
#>
dat <- structure(list(
Gender = rep_len(c("Women","Men"), length.out = 130),
Education = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Basic", "Medium", "Higher"), class = "factor"),
Efficacy = structure(c(24, 24, 24, 24, 24, 24, 24, 24, 29, 29, 29, 29, 28, 28, 28, 28, 29, 29, 29, 29, 22, 22, 22, 22, 30, 30, 30, 30, 26, 26, 26, 26, 16, 16, 16, 16, 22, 22, 22, 22, 26, 26, 26, 26, 30, 30, 30, 30, 32, 32, 32, 32, 20, 20, 20, 20, 24, 24, 24, 24, 31, 31, 31, 31, 28, 28, 28, 28, 26, 26, 26, 26, 25, 25, 25, 25, 35, 35, 35, 35, 30, 30, 30, 30, 25, 25, 25, 25, 28, 28, 28, 28, 33, 33, 33, 33, 33, 33, 33, 33, 22, 22, 22, 22, 24, 24, 24, 24, 33, 33, 33, 33, 27, 27, 27, 27, 35, 35, 35, 35, 26, 26, 26, 26, 22, 22, 22, 22, 25, 25 ), format.spss = "F16.2"),
grp = c("Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring"),
points = c(9, 20, 12, 15, 10, 22, 8, 14, 12, 25, 11, 17, 10, 21, 12, 18, 13, 22, 12, 16, 11, 11, 8, 11, 10, 21, 14, 20, 8, 19, 5, 18, 5, 15, 7, 11, 6, 18, 12, 16, 7, 23, 10, 19, 8, 24, 15, 22, 12, 19, 10, 20, 6, 20, 7, 14, 9, 20, 7, 15, 10, 22, 7, 21, 9, 19, 13, 19, 11, 24, 7, 15, 9, 22, 9, 16, 13, 22, 12, 22, 11, 24, 13, 19, 10, 19, 11, 15, 10, 19, 11, 18, 14, 25, 15, 19, 13, 25, 7, 20, 9, 22, 5, 13, 8, 19, 5, 16, 11, 16, 10, 22, 7, 24, 12, 20, 13, 24, 13, 22, 6, 22, 4, 20, 8, 17, 8, 14, 10, 19)), row.names = c(NA, -130L), class = c("tbl_df", "tbl", "data.frame")
)
t_test(dat, points ~ Gender, ref.group = "Women", alternative = "less")
#> # A tibble: 1 × 8
#> .y. group1 group2 n1 n2 statistic df p
#> * <chr> <chr> <chr> <int> <int> <dbl> <dbl> <dbl>
#> 1 points Women Men 65 65 -17.0 120. 6.1e-34
Created on 2023-03-16 with reprex v2.0.2
If I understand your write-up correctly, you want to perform this t-test for each grp
in the data frame. That still won't work because we run into the same error due to not having multiple genders in each grp
:
library(tidyverse, quietly = TRUE)
library(rstatix, quietly = TRUE)
#>
dat <- structure(list(
Gender = rep_len(c("Women","Men"), length.out = 130),
Education = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Basic", "Medium", "Higher"), class = "factor"),
Efficacy = structure(c(24, 24, 24, 24, 24, 24, 24, 24, 29, 29, 29, 29, 28, 28, 28, 28, 29, 29, 29, 29, 22, 22, 22, 22, 30, 30, 30, 30, 26, 26, 26, 26, 16, 16, 16, 16, 22, 22, 22, 22, 26, 26, 26, 26, 30, 30, 30, 30, 32, 32, 32, 32, 20, 20, 20, 20, 24, 24, 24, 24, 31, 31, 31, 31, 28, 28, 28, 28, 26, 26, 26, 26, 25, 25, 25, 25, 35, 35, 35, 35, 30, 30, 30, 30, 25, 25, 25, 25, 28, 28, 28, 28, 33, 33, 33, 33, 33, 33, 33, 33, 22, 22, 22, 22, 24, 24, 24, 24, 33, 33, 33, 33, 27, 27, 27, 27, 35, 35, 35, 35, 26, 26, 26, 26, 22, 22, 22, 22, 25, 25 ), format.spss = "F16.2"),
grp = c("Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring"),
points = c(9, 20, 12, 15, 10, 22, 8, 14, 12, 25, 11, 17, 10, 21, 12, 18, 13, 22, 12, 16, 11, 11, 8, 11, 10, 21, 14, 20, 8, 19, 5, 18, 5, 15, 7, 11, 6, 18, 12, 16, 7, 23, 10, 19, 8, 24, 15, 22, 12, 19, 10, 20, 6, 20, 7, 14, 9, 20, 7, 15, 10, 22, 7, 21, 9, 19, 13, 19, 11, 24, 7, 15, 9, 22, 9, 16, 13, 22, 12, 22, 11, 24, 13, 19, 10, 19, 11, 15, 10, 19, 11, 18, 14, 25, 15, 19, 13, 25, 7, 20, 9, 22, 5, 13, 8, 19, 5, 16, 11, 16, 10, 22, 7, 24, 12, 20, 13, 24, 13, 22, 6, 22, 4, 20, 8, 17, 8, 14, 10, 19)), row.names = c(NA, -130L), class = c("tbl_df", "tbl", "data.frame")
)
dat |>
filter(grp == "Avoiding") |>
t_test(points ~ Gender, ref.group = "Women", alternative = "less")
#> Error in t.test.default(x = c(9, 10, 12, 10, 13, 11, 10, 8, 5, 6, 7, 8, : not enough 'y' observations
Created on 2023-03-16 with reprex v2.0.2 Created on 2023-03-16 with reprex v2.0.2
We can fix that by modifying Gender
further to have at least two genders represented in each grp
:
library(tidyverse, quietly = TRUE)
library(rstatix, quietly = TRUE)
#>
dat <- structure(list(
Gender = rep_len(c("Women", "Women", "Women", "Women", "Men", "Men", "Men", "Men"), length.out = 130),
Education = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Basic", "Medium", "Higher"), class = "factor"),
Efficacy = structure(c(24, 24, 24, 24, 24, 24, 24, 24, 29, 29, 29, 29, 28, 28, 28, 28, 29, 29, 29, 29, 22, 22, 22, 22, 30, 30, 30, 30, 26, 26, 26, 26, 16, 16, 16, 16, 22, 22, 22, 22, 26, 26, 26, 26, 30, 30, 30, 30, 32, 32, 32, 32, 20, 20, 20, 20, 24, 24, 24, 24, 31, 31, 31, 31, 28, 28, 28, 28, 26, 26, 26, 26, 25, 25, 25, 25, 35, 35, 35, 35, 30, 30, 30, 30, 25, 25, 25, 25, 28, 28, 28, 28, 33, 33, 33, 33, 33, 33, 33, 33, 22, 22, 22, 22, 24, 24, 24, 24, 33, 33, 33, 33, 27, 27, 27, 27, 35, 35, 35, 35, 26, 26, 26, 26, 22, 22, 22, 22, 25, 25 ), format.spss = "F16.2"),
grp = c("Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring"),
points = c(9, 20, 12, 15, 10, 22, 8, 14, 12, 25, 11, 17, 10, 21, 12, 18, 13, 22, 12, 16, 11, 11, 8, 11, 10, 21, 14, 20, 8, 19, 5, 18, 5, 15, 7, 11, 6, 18, 12, 16, 7, 23, 10, 19, 8, 24, 15, 22, 12, 19, 10, 20, 6, 20, 7, 14, 9, 20, 7, 15, 10, 22, 7, 21, 9, 19, 13, 19, 11, 24, 7, 15, 9, 22, 9, 16, 13, 22, 12, 22, 11, 24, 13, 19, 10, 19, 11, 15, 10, 19, 11, 18, 14, 25, 15, 19, 13, 25, 7, 20, 9, 22, 5, 13, 8, 19, 5, 16, 11, 16, 10, 22, 7, 24, 12, 20, 13, 24, 13, 22, 6, 22, 4, 20, 8, 17, 8, 14, 10, 19)), row.names = c(NA, -130L), class = c("tbl_df", "tbl", "data.frame")
)
dat |>
filter(grp == "Avoiding") |>
t_test(points ~ Gender, ref.group = "Women", alternative = "less")
#> # A tibble: 1 × 8
#> .y. group1 group2 n1 n2 statistic df p
#> * <chr> <chr> <chr> <int> <int> <dbl> <dbl> <dbl>
#> 1 points Women Men 17 16 -0.568 30.9 0.287
Created on 2023-03-16 with reprex v2.0.2
You don't actually need to use map()
to do a t-test for each group once the data is valid:
library(tidyverse, quietly = TRUE)
library(rstatix, quietly = TRUE)
#>
dat <- structure(list(
Gender = rep_len(c("Women", "Women", "Women", "Women", "Men", "Men", "Men", "Men"), length.out = 130),
Education = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Basic", "Medium", "Higher"), class = "factor"),
Efficacy = structure(c(24, 24, 24, 24, 24, 24, 24, 24, 29, 29, 29, 29, 28, 28, 28, 28, 29, 29, 29, 29, 22, 22, 22, 22, 30, 30, 30, 30, 26, 26, 26, 26, 16, 16, 16, 16, 22, 22, 22, 22, 26, 26, 26, 26, 30, 30, 30, 30, 32, 32, 32, 32, 20, 20, 20, 20, 24, 24, 24, 24, 31, 31, 31, 31, 28, 28, 28, 28, 26, 26, 26, 26, 25, 25, 25, 25, 35, 35, 35, 35, 30, 30, 30, 30, 25, 25, 25, 25, 28, 28, 28, 28, 33, 33, 33, 33, 33, 33, 33, 33, 22, 22, 22, 22, 24, 24, 24, 24, 33, 33, 33, 33, 27, 27, 27, 27, 35, 35, 35, 35, 26, 26, 26, 26, 22, 22, 22, 22, 25, 25 ), format.spss = "F16.2"),
grp = c("Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring", "Coping", "Obtaining", "Avoiding", "Savoring"),
points = c(9, 20, 12, 15, 10, 22, 8, 14, 12, 25, 11, 17, 10, 21, 12, 18, 13, 22, 12, 16, 11, 11, 8, 11, 10, 21, 14, 20, 8, 19, 5, 18, 5, 15, 7, 11, 6, 18, 12, 16, 7, 23, 10, 19, 8, 24, 15, 22, 12, 19, 10, 20, 6, 20, 7, 14, 9, 20, 7, 15, 10, 22, 7, 21, 9, 19, 13, 19, 11, 24, 7, 15, 9, 22, 9, 16, 13, 22, 12, 22, 11, 24, 13, 19, 10, 19, 11, 15, 10, 19, 11, 18, 14, 25, 15, 19, 13, 25, 7, 20, 9, 22, 5, 13, 8, 19, 5, 16, 11, 16, 10, 22, 7, 24, 12, 20, 13, 24, 13, 22, 6, 22, 4, 20, 8, 17, 8, 14, 10, 19)), row.names = c(NA, -130L), class = c("tbl_df", "tbl", "data.frame")
)
dat |>
group_by(grp) |>
t_test(points ~ Gender, ref.group = "Women", alternative = "less")
#> # A tibble: 4 × 9
#> grp .y. group1 group2 n1 n2 statistic df p
#> * <chr> <chr> <chr> <chr> <int> <int> <dbl> <dbl> <dbl>
#> 1 Avoiding points Women Men 17 16 -0.568 30.9 0.287
#> 2 Coping points Women Men 16 16 0.113 29.8 0.544
#> 3 Obtaining points Women Men 16 16 0.275 26.7 0.607
#> 4 Savoring points Women Men 17 16 0.610 27.5 0.727
Created on 2023-03-16 with reprex v2.0.2
Please take the time to create a reproducible question, it's worth the investment. You'll get better answers more quickly and it will help you reason through your question. Refer to this helpful guide and / or use reprex when working with R because it makes it way easier.