Yet another way.
is.na(dat$Population_and_People.X__76) <- dat$Population_and_People.X__76 == "-"
Followed by mean
with na.rm = TRUE)
.
EDIT
Note that your column is probably of class factor
. A vetcor can only have one type of data if it has a character such as "-", the entire column will be transformed to class character
in the first step and then to factor
. This last step is the default behaviour, you must set stringsAsFactors = FALSE
in order for it not to happen. The (not so) pratical result is that you cannot use mean
on that column. You will most probably need to do
dat$Population_and_People.X__76 <- as.numeric(as.character(dat$Population_and_People.X__76))
Before you do this check the class of that column, either with class(dat$Population_and_People.X__76)
or with str(dat)
.