Calculate the mean

What is the mean of the Ozone column in this dataset? Exclude missing values (coded as NA) from this calculation.

There is no information in the Coursera lecture notes on how to calculate the mean from a column of data. I Google search for “R calculate mean exclude missing values”:

Some helpful links and info:


Excluding Missing Values from Analyses

Arithmetic functions on missing values yield missing values.

x <- c(1,2,NA,3)
mean(x) # returns NA
mean(x, na.rm=TRUE) # returns 2

The function complete.cases() returns a logical vector indicating which cases are complete.

# list rows of data that have missing values

The function na.omit() returns the object with listwise deletion of missing values.

# create new dataset without missing data
newdata <- na.omit(mydata)


The following series of commands end up working:



This produced an integer matrix of 153 values, which includes missing values.



This puts the data back into another format. An attempt to take the mean of the column results in an error (below). So, I put them back into a matrix with another u<-cbind(u) command.


The result is this, which is the correct answer for the quiz:



About Lisa Cohen

PhD student at UC Davis.
This entry was posted in Coursera, R. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s