selender14
selender14

Reputation: 21

How to perform chi-squared test in R in my dataset?

I have the following dataset on I would like to perform chi-squared test, since I am curious whether is there any significant difference between the number of males and females with different genotypes. I've tried several solution (I'm not writing it down because they were written for an other form of this dataset) but it did not work. I was adviced to input my data as the followings.

genotype males females
1    blm_wt    14       6
2   blm_het    33      11
3  blm_wt_2     9       7
4 blm_het_2    36      10

My code looks like this right now:

library(tidyverse)
library(ggplot2)
library(ggpubr)
library(ggsci)
library(ggthemes)
library(ggExtra)
blm_sex_chi = read.csv("D:/Krisztiann/Antropologia/R_programozas/zebrahal/blm_sex_ratios_table_chi.csv", header = TRUE) 
#with this I'm reading the csv file which looks like as it was shown above
chi.res = chisq.test(blm_sex_chi)

I'm getting this error message: Error in chisq.test(blm_sex_chi) : all entries of 'x' must be nonnegative and finite.

I don't know why. My dataset looks like this guy's in this video (https://www.youtube.com/watch?v=POiHEJqmiC0) but it does not work. Maybe the problem is that mine was not transformed to contingency table. I tried but the function just messed up the table.

I'm very beginner in R, do you have any for this problem?

Thx in advance!

Upvotes: 2

Views: 1624

Answers (1)

TarJae
TarJae

Reputation: 78907

Bring your dataframe in a format of contingency table:

For this remove the existing rownames (1,2,3,4) by using as_tibble and add the column genotype as rownames:

library(dplyr)
library(tibble)
df1 <- df %>% 
  as_tibble() %>% 
  column_to_rownames("genotype")

chisq <- chisq.test(df1)
chisq
    Pearson's Chi-squared test

data:  df1
X-squared = 3.1052, df = 3, p-value = 0.3757

Upvotes: 3

Related Questions