Error loading an average column [closed]

-1
  • I changed the NA value by zero, changing it into a character because I could not calculate the mean.
  • When trying to calculate the average again the following error appeared:
  • Warning message:
      In mean.default(x = "ECONOMIA INSTITUCIONALISTA") :
    argumento não é numérico nem lógico: retornando NA
    
        
    asked by anonymous 27.04.2018 / 03:43

    1 answer

    2
    # para que os resultados sejam reproduzíveis
    set.seed(42)
    
    # gerar os dados
    x <- rnorm(10)
    x
    # [1]  1.37095845 -0.56469817  0.36312841  0.63286260  0.40426832 -0.10612452
    # [7]  1.51152200 -0.09465904  2.01842371 -0.06271410
    
    # adicionando NA's para exemplo
    x[c(3, 5, 8)] <- NA
    x
    # [1]  1.3709584 -0.5646982         NA  0.6328626         NA -0.1061245
    # [7]  1.5115220         NA  2.0184237 -0.0627141
    

    Using only the mean() function, it recognizes the presence of NA in the vector and then returns a NA :

    mean(x)
    # [1] NA
    

    Important : Considering NA as zero (0) or NA will change the result. It is important that you know this and then choose the most appropriate method for your goals.

    # média considerando NA
    mean(x, na.rm = T)
    # [1] 0.6857471
    
    # média considerando NA como zero
    x[which(is.na(x))] <- 0
    mean(x)
    # [1] 0.480023 
    
        
    27.04.2018 / 09:40