Pearson Correlation -- NA

Hi community! I am working with Pearson Correlation and when applying it to a data.frame, R Studio returns me only "NA" values. I really don´t know the reason of these NAs as all my data are numbers. To expose my issue I present a short reprex:

data.frame(
             Numero_Vuelos = c(884620),
               Num_Ascenso = c(201303),
              Num_Descenso = c(262967),
               Num_Crucero = c(420355),
                Num_Manana = c(405628),
                 Num_Tarde = c(301654),
                 Num_Noche = c(168953),
                  FL_Medio = c(277.306283476358),
              Tiempo_Medio = c(147.109976918405),
                Long_Media = c(16.8599598101687),
                     Heavy = c(111008),
                    Medium = c(750414),
                     Light = c(23058),
                       L_M = c(145),
        Trafico_Enfrentado = c(583010),
   Numero_Conflictos_MISMO = c(82),
   Numero_Conflictos_Entre = c(2)
)
#>   Numero_Vuelos Num_Ascenso Num_Descenso Num_Crucero Num_Manana Num_Tarde
#> 1        884620      201303       262967      420355     405628    301654
#>   Num_Noche FL_Medio Tiempo_Medio Long_Media  Heavy Medium Light L_M
#> 1    168953 277.3063       147.11   16.85996 111008 750414 23058 145
#>   Trafico_Enfrentado Numero_Conflictos_MISMO Numero_Conflictos_Entre
#> 1             583010                      82                       2

datos.cor<-cor(datos,method="pearson")
#> Error in is.data.frame(x): objeto 'datos' no encontrado
round(datos.cor,digits=2)
#> Error in eval(expr, envir, enclos): objeto 'datos.cor' no encontrado
corrplot(datos.cor)
#> Error in corrplot(datos.cor): no se pudo encontrar la función "corrplot"

Solution to this reprex was a matrix with all NA in rows and columns.

Does anyone know how to solve this issue? thanks in advance for your help and happy new year!

@PalomaLlorente To compute correlation, you need at least two variables. Your data frame has only one value in each column meaning that these are constants...that's why you're getting NA as the output. For instance:

this_works <- data.frame(A = c(2.0, 3.5, 4.0), B = c(3.5, 6.0, 9.0))
print(this_works)
    A   B
1 2.0 3.5
2 3.5 6.0
3 4.0 9.0

cor(this_works, method = "pearson")
          A         B
A 1.0000000 0.9449112
B 0.9449112 1.0000000

this_wont_work <- data.frame(A = 2.0, B = 3.5)
print(this_wont_work)
  A   B
1 2 3.5

cor(this_wont_work, method = "pearson")
   A  B
A NA NA
B NA NA

Moreover, there are a couple of other issues in your reprex.

  1. You need to assign your data frame to the variable datos. That's why you're getting the object not found error.

  2. corrplot is not a base R function. You need to load the corrplot package first with library(corrplot) before calling the function.

Please provide a proper reprex of your data so that we can help you.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.