# Test for significant differences on gender between two groups in R

I need to compare the significance of Gender between the two groups. Here is the table:

df <- data.frame(No = c(1:15),
Group = sample(1:2, 15, replace=T),
Gender = sample(c("F","M"), 15, replace=T))

Ge_M <- df %>%
filter(Gender == "M") %>% group_by(Group) %>%
summarise(Value = n()) %>%
pull(Value)

Ge_F <- df %>%
filter(Gender == "F") %>% group_by(Group) %>%
summarise(Value = n()) %>%
pull(Value)

t.test(Ge_M, Ge_F)


Output :

Welch Two Sample t-test

data:  Ge_M and Ge_F
t = -0.82199, df = 1.0555, p-value = 0.5561
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-36.65746  31.65746
sample estimates:
mean of x mean of y
2.5       5.0


Question: Am I using it in the right way? I want to know if I have significant a difference between the two groups but the output seems not what I want.

What's the right method to do the test?

Thank you!

• Using your code it works for me, data: Value and Group F = 2.6127, num df = 1, denom df = 18, p-value = 0.1234. May 4, 2023 at 5:56
• You have many zeros in your data. What is the explanation for them? Looking at the data I have doubts that the assumptions of a t-test are sufficenctly fulfilled. May 4, 2023 at 7:08

It is not clear to me what you are trying to do here since the new df misses the response (see below). But one critique may be that you may want to check for the homoscedasticity assumption first, using say var.test or any other test for equality of variances. And if the homoscedasticity assumption cannot be rejected, then you can use a t-test with var.equal = TRUE.

Answer to the original version of the post

The problem here is with the use of df which is an R function (df computes the density of an F distribution, run ?df to check it). Using another name, say dd, fixes the issue. Note that in the case of two groups, you can also use the $$t$$-test.

dd <- read.table("data.txt", header = F,row.names = 1)
names(dd) <- c("ID", "group", "value")
str(dd)

> oneway.test(value ~ group, data = dd, var.equal = TRUE)

One-way analysis of means

data:  value and group
F = 2.6127, num df = 1, denom df = 18, p-value = 0.1234

# or the equivalent version via t-test
> with(dd, t.test(value~group, var.equal = TRUE))

Two Sample t-test

data:  value by group
t = -1.6164, df = 18, p-value = 0.1234
alternative hypothesis: true difference in means between group 1 and group 2 is not equal to 0
95 percent confidence interval:
-8.580329  1.118425
sample estimates:
mean in group 1 mean in group 2
2.385714        6.116667


Remark Furthermore, as also highlighted in the comments by Roland, value under group 1 has many zeros, i.e.

> with(dd, table(value[group == 1]))

0    2  4.3  6.6  8.3 12.2
9    1    1    1    1    1


This casts doubts on the normality of such a variable and thus the p-value of the t-test (or ANOVA) may not be correct.