# [R] Calculating the mean in one column with empty cells

arun smartpink111 at yahoo.com
Fri Oct 5 16:21:29 CEST 2012

```HI,
Not sure how your dataset looks like:

set.seed(1)
dat1<-data.frame(col1=c(sample(1:50,5,replace=TRUE),NA,sample(1:25,3,replace=TRUE),NA,sample(1:25,2,replace=TRUE)),col2=c(NA,NA,rnorm(8,15),NA,NA))
mean(dat1\$col1[!is.na(dat1\$col1)])
# 20.1
mean(dat1\$col1,na.rm=TRUE)
# 20.1

But, there is one problem that is obvious "dataset2" and "master".  Looks like you have two datasets.
A.K.

----- Original Message -----
From: fxen3k <f.sehardt at gmail.com>
To: r-help at r-project.org
Cc:
Sent: Friday, October 5, 2012 5:27 AM
Subject: [R] Calculating the mean in one column with empty cells

Hi all,

I recently tried to calculate the mean and the median just for one column.
In this column I have numbers with some empty cells due to missing data.
So how can I calculate the mean just for the filled cells?
I tried:
mean(dataSet2\$ac_60d_4d_after_ann[!is.na(master\$ac_60d_4d_after_ann)],
na.rm=TRUE)
But the output was different to the calculation I died in Microsoft Excel.

Felix

--
View this message in context: http://r.789695.n4.nabble.com/Calculating-the-mean-in-one-column-with-empty-cells-tp4645135.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help