简体   繁体   中英

Trying to delete rows with NAs, so that I can do chi-square analysis

R community.

I am trying to delete the rows TEMPORARILY that have NA, so that I may conduct a chi-square analysis on my data. Otherwise, there would be a column titled "unknown" when I choose two variable to make a data table, and that would interfere with the chi-square analysis from what I know.

The rows correspond to each patient in the data. mint$bp_type2 and mint$menopause are two columns. My thought was to replace the NAs with a random number; I chose 20. That way, I would be able to delete these rows that had a value of 20 under that column.

Here is the code:

mint <- mint[!(mint$bp_type2 == 20),]
mint$menopause[is.na(mint$menopause)] <- 20
mint <- mint[!(mint$menopause == 20),]
table(mint$bp_type2,mint$menopause)
chisq.test(mint$bp_type2,mint$menopause)

This is my error message:

> mint$bp_type2[is.na(mint$bp_type2)] <- 20
Warning message:
In `[<-.factor`(`*tmp*`, is.na(mint$bp_type2), value = c(1L, 4L,  :
  invalid factor level, NA generated

Subsequently, I run the rest of the code, and I see that the data set has not eliminated these rows, since the data table generated still has a column labeled "unknown."

Please let me know what I can do to fix this, so that I can do my chi-square analysis without an "unknown" column.

Thank you!!

The error message is because the column 'bp_type2' is a factor and '20' is not one of the levels of that column. We can either convert to a character or create '20' as one of the levels

levels(mint$bp_type2) <- c(levels(mint$bp_type2), '20')

and then do the assignment

mint$bp_type2[is.na(mint$bp_type2)] <- '20'

With forcats , fct_expand can add more levels

library(forcats)
mint$bp_type2 <- fct_expand(mint$bp_type2, '20')

Try:

chisq.test(na.omit(df$columnName))

This will omit the NAs while proeceding the Chi-Square Test.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM