问题
Example of some entries in the data frame:
I need to find the mean of this column in the data frame, but can't find the mean as it says:
" argument is not numeric or logical: returning NA"
The non-numeric entries are dash signs, I have tried converting them to NA but still am struggling to produce a result for the mean.
Can anyone help?
回答1:
Try this, assuming your data is called dat
:
dat[dat == "-"] <- NA
mean(dat$Population_and_People, na.rm = TRUE]
回答2:
This isn't using the supplied data but should be enough to show the desired result. Note this is related to How to avoid warning when introducing NAs by coercion
x <- c("5", "-", "15")
mean(suppressWarnings(as.numeric(as.character(x))), na.rm = TRUE)
#> [1] 10
回答3:
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)
.
回答4:
Try this:
dataset$Population_and_People.X_76 <- gsub("-", NA, dataset$Population_and_People.X_76], fixed=TRUE)
dataset$Population_and_People.X_76 <- as.numeric(dataset$Population_and_People.X_76)
mean(dataset$Population_and_People.X_76, na.rm=TRUE)
This will not account for treated records(hyphens) in the denominator while calculating mean.
来源:https://stackoverflow.com/questions/46211779/r-how-to-find-the-mean-of-a-column-in-a-data-frame-that-has-non-numeric-speci