Mean function not giving the correct answer in R

r
statistics

#1

I wrote a command to calculate mean

mean(2,20,26)

and I am getting the output as

[1] 2

Why is this happening and why is the first value getting returned instead of the mean?


#2

hello @pravin,

If you are using R then you have to use:

mean(c(2,20,26))

Hope this helps!!


#3

@pravin, if you look at the mean function’s help page (?mean from the R coammnd line), you’ll notice that the function calculates the mean of a particular R object which should be provided as the first argument (if you are not using keywords). In you case it happens to be a numeric–a single digit, whereas what you wish to find out is the mean of 2, 20 and 26. So to do that you must provide them in a concatenated form (i.e. a vector), as shown by @shuvayan.

And remember, always RTM. :smile:

(However, I’m not sure about why it does not complain about the validity, of lack thereof, of the other two arguments.)