R:如何在具有非数字(特别是破折号“-")和数字的数据框中查找列的平均值 [英] R: How to find the mean of a column in a data frame, that has non-numeric (specifically, dashes '-') as well as numeric numbers

查看:38
本文介绍了R:如何在具有非数字(特别是破折号“-")和数字的数据框中查找列的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据框中一些条目的示例:

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:

" 参数不是数字或逻辑:返回 NA"

" argument is not numeric or logical: returning NA"

非数字条目是破折号,我已尝试将它们转换为 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.

有人可以帮忙吗?

推荐答案

试试这个,假设你的数据叫做 dat:

Try this, assuming your data is called dat:

dat[dat == "-"] <- NA

mean(dat$Population_and_People, na.rm = TRUE]

这篇关于R:如何在具有非数字(特别是破折号“-")和数字的数据框中查找列的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆