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:

[1] http://www.statmethods.net/input/missingdata.html

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
mydata[!complete.cases(mydata),]

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

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

[2] https://stat.ethz.ch/pipermail/r-help/2007-November/146886.html

The following series of commands end up working:

ProgrammingQuizW1_data_mean_subsetcolumn_attempt

ProgrammingQuizW1_data_missingvalues_attempt

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

Then,

ProgrammingQuizW1_data_missingvalues_attempt2

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.

ProgrammingQuizW1_data_missingvalues_colmeans

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

ProgrammingQuizW1_data_missingvalues_works_sortof

Advertisements

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:

WordPress.com Logo

You are commenting using your WordPress.com 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