# 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
Please read the answer accepted in this Meta Downvoting of new user questions
– Rui Barradas
NA
by zero, let alone turn intocharacter
. 2) The error says that"ECONOMIA INSTITUCIONALISTA"
is not a number orFALSE/TRUE
, How do you want to average that?!? 3) Put the output ofdput(dados)
in the question or ifdados
is very large, ofdput(head(dados, 20))
.– Rui Barradas