如何在R中找到列的均值 [英] How to find the mean of a column in R
本文介绍了如何在R中找到列的均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我正在使用的csv文件.
Here is my csv file I'm using.
my.xldataset <- read.csv('http://www.math.smith.edu/sasr/datasets/help.csv')
这是我尝试查找"mcs1"列的均值.
Here's my attempt at finding the mean of column "mcs1".
mean(my.xldataset$mcs1)
我得到的只是一个"NA".我到底哪里出问题了?谢谢
All I'm getting in return is an "NA". Where exactly am I going wrong here? Thank you
推荐答案
该列中可能有 NA
值,因此请使用 na.rm = TRUE
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)
或
class(my.xldataset$mcs1)
通过检查数据集,
By checking the dataset,
any(is.na(my.xldataset$mcs1))
#[1] TRUE
NA
元素确实在数据集中.因此,请使用 na.rm = TRUE
.
the NA
elements are indeed in the dataset. So, use the na.rm=TRUE
.
这篇关于如何在R中找到列的均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文