How to find the mean of a column in R [duplicate]

ε祈祈猫儿з 提交于 2020-12-27 07:19:28

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!