-2

Here is my csv file I'm using.

my.xldataset <- read.csv('http://www.math.smith.edu/sasr/datasets/help.csv')

Here's my attempt at finding the mean of column "mcs1".

mean(my.xldataset$mcs1)

All I'm getting in return is an "NA". Where exactly am I going wrong here? Thank you

Tomas
  • 115
  • 1
  • 2
  • 12

2 Answers2

3

It could be that there are NA values in the column, so use na.rm=TRUE

mean(my.xldataset$mcs1, na.rm=TRUE)

or it could be that the column is not numeric. In that case, check the

str(my.xldataset)

or

class(my.xldataset$mcs1)

By checking the dataset,

any(is.na(my.xldataset$mcs1))
#[1] TRUE

the NA elements are indeed in the dataset. So, use the na.rm=TRUE.

akrun
  • 874,273
  • 37
  • 540
  • 662
3

As @akrun noted, it is probably because of NA in that column of data. You can also run:

summary(my.xldataset$mcs1)

which will report min, max, median, quartiles etc... as well as give you the number of NA's :)

   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
  6.677  30.210  42.440  40.980  52.730  69.940     207
Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
akaDrHouse
  • 2,190
  • 2
  • 20
  • 29
  • Why the downvotes? The mean is listed in the output from summary(). The output is posted above. Not only does it provide the mean, but it does so with NA's, and reports the number of NAs. How does this not answer the OP's question? – akaDrHouse Jul 07 '16 at 14:38
  • You are right. `summary` does include the mean. I removed my down vote. – Rich Scriven Jul 08 '16 at 03:54