Error loading average of a column

Asked

Viewed 114 times

-1

  1. I switched the value of NA to zero, turning it into Character because I couldn’t calculate the average.
  2. When trying to calculate the average again appeared the following error:
Warning message:
  In mean.default(x = "ECONOMIA INSTITUCIONALISTA") :
argumento não é numérico nem lógico: retornando NA
  • 2

    Please read the answer accepted in this Meta Downvoting of new user questions

    1. No need to change the value of NA by zero, let alone turn into character. 2) The error says that "ECONOMIA INSTITUCIONALISTA" is not a number or FALSE/TRUE, How do you want to average that?!? 3) Put the output of dput(dados) in the question or if dados is very large, of dput(head(dados, 20)).

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 function mean() he recognizes the presence of NA in the vector and then returns a NA:

mean(x)
# [1] NA

Important: consider the NA as zero (0) or NA will change the result. It is important that you know this so 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 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.