问题
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
回答1:
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
.
回答2:
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
来源:https://stackoverflow.com/questions/37908949/how-to-find-the-mean-of-a-column-in-r