R: How to find the mean of a column in a data frame, that has non-numeric (specifically, dashes '-') as well as numeric numbers [closed]

为君一笑 提交于 2021-01-08 08:54:54

问题


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 characterin 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!