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

Ken Takagi katakagi at bu.edu
Fri Oct 5 15:04:19 CEST 2012


fxen3k <f.sehardt <at> gmail.com> writes:

> 
> 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.
> 
> Thanks in advance,
> 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.
> 
> 

Hi Felix,

Assuming that you have the data frame formatted properly, mean(yourdata$column,
na.rm = T) should work.  When coverting an excel table to R, I usually fill in
blank cells with NA before importing.  If you try to import an data frame with
empty cells, you usually get an error using read.table().  But since you seem to
have already got you data into R, that may not be the problem.

HTH,
Ken




More information about the R-help mailing list