2

So I have this vector:

x
 [1] 76 89 78 50 84 56 29 53 32 68
112 Levels: 0 1 10 100 101 102 103 104 105 106 107 108 109 11 110 12 13 ... eta

why this happens?

[1] NA
Warning message:
In mean.default(x) : l'argomento non è numerico o logico: restituisco NA
user3083324
  • 565
  • 8
  • 22
  • 1
    Trying to translate the error message: "the argument is neither numerical nor logical. Replaced with N/A". – Floris Dec 09 '13 at 17:45
  • 1
    You can set the language to English using [this](http://stackoverflow.com/questions/13575180/how-to-change-the-language-of-errors-in-r) – agstudy Dec 09 '13 at 17:52

3 Answers3

7

It looks like x is a factor. There is a gotcha when converting factors to numbers. You need to use:

mean(as.numeric(as.character(x)), na.rm=TRUE)

If you don't convert to character first, you will get the underlying factor codes.

James
  • 63,608
  • 14
  • 148
  • 190
0

Looks like x is a categorical variable -- try

mean(as.numeric(as.character(x)))

(as per James pointing out that without as.character, you get factor codes:

x <- as.factor(10:20)
as.numeric(x)
[1]  1  2  3  4  5  6  7  8  9 10 11

Leaving out na.rm=TRUE since, while safer, it wasn't causing the problem)

colcarroll
  • 3,542
  • 15
  • 25
0

In addition to changing to numeric values, when you use the mean function, make sure you specify na.rm=TRUE in case you have NA values in the vector. Otherwise, it shows the warning you saw.

alittleboy
  • 9,996
  • 22
  • 60
  • 105