Home > Mobile >  Pearson correlation test giving NA
Pearson correlation test giving NA

Time:11-18

I tried to get a Pearson correlation for this data, but it resulted in NA. What goes wrong?

My code: correlation <- cor(df$var1, df$var2, method = 'pearson')

Data

structure(list(var1 = c(2, 2, 1, 1, 1, 1, 1, 1, 1, 
2, 1, 1, 2, 1, 1, 1, 2, NA, 1, 1, 1, 1, 2, 2, NA, 2, 1, 2, 1, 
1, 1, 1, 2, 1, 3, 3, 2, 2, 1, 1, 1, NA, 2, 3, 1, 1, 2, 2, 2, 
NA), var2= c(2, 1, 1, 2, 2, 1, 1, 1, 1, 3, 2, 1, 2, 
1, 1, 1, 1, NA, 3, 1, 1, 1, 2, 2, NA, 3, 1, 1, 2, 2, 1, 1, 1, 
2, 2, 3, 2, 3, 4, 1, 3, NA, 4, 2, 4, 1, 2, 2, 2, NA)), class = c("rowwise_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -50L), groups = structure(list(
    .rows = structure(list(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
        10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 
        21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 
        32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 
        43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, -50L), class = c("tbl_df", 
"tbl", "data.frame")))

CodePudding user response:

It's missing NA values in your dataset. This should work:

cor(df$var1, df$var2, method = 'pearson', use = "complete")
  • Related