当前位置:网站首页>缺失值处理
缺失值处理
2022-06-22 16:04:00 【R语言爱好者】
前言
在日常的统计分析中,数据缺失的现象是常见的,可能是在一场数据录入中导致数据缺失,或者是在问答中没有得到自己想要的结果等,都会造成数据的部分缺失。数据缺失的处理方式也有很多,需要我们根据实际的确实情况进行解决。当数据缺失量在整体中的占比比较小时,我们可以直接删除,但是如果该数据对结果影响较为显著,那么我们则不能删除,需要对数据进行填补。
1、缺失值的检测?
在R语言中,我们的缺失值的符号时用NA来表示的,并且在R中,用函数is.na()可以来检测数据是否存在缺失值。
na <- c(2,2,NA,6,8)
is.na(na)
结果:
使用is.na()函数来判断是否存在缺失值,返回的结果时一个逻辑值,也就是有或者没有缺失值。当返回的逻辑值为FALSE时为没有缺失值,当返回值为TURE时为缺失值。
2、NaN的检测
NaN表示不能被表示的值,不确定性的值,可以用is.nan()来检测
nan <- c(5,Inf,5,NaN,6)
is.nan(nan)
结果
3、缺失值的处理
借补发:
new_na <- c(1,1,2,3,NA,7,8)
new_na[is.na(new_na)] <- mean(new_na,na.rm=T)
结果

4、选取不是缺失值的数
加入一个数据中存在缺失值,那么我们时无法进行计算的,因此我们需要滤过缺失值才能计算。那么我们只要选出不是缺失值的数就可以了。
new_nas <- c(1,NA,1,2,3,NA,7,8)
new_nas <- new_nas[!is.na(new_nas)]
结果:
5、矩阵中带缺失值
在矩阵总也可以带缺失值,但是同样是不可以进行计算的,我们需要将缺失值处理后才可以进行计算。
mat <- matrix(c(1:11,NA),nr=3,nc=4,byrow = T)
结果
6、不算缺失值
mean(mat,na.rm=T)
结果
7、删除所在行
当我们删除了NA值所在的行时,矩阵中便没有了缺失值,因此这个矩阵式可以进行计算的。
newmat <- na.omit(mat)
结果
8、求均值
我们对矩阵里面的数求一个均值,可以得到以下结果。
mean(newmat)
结果
总结
1:is.na()可以检测缺失值,并返回逻辑向量;
2:is.nan()可以检测NaN值,并返回逻辑向量;
3:含有缺失值的是不可以进行计算的。
边栏推荐
- Quartus Prime 18.0软件安装包和安装教程
- imx6ull的GPIO操作方法
- ABP Framework 5.3.0 版本新增功能和变更说明
- Come to Xiamen! Online communication quota free registration
- Hello playwright: (7) simulate keyboard and mouse
- 推荐7款超级好用的终端工具 —— SSH+FTP
- . Net release and support plan introduction
- Figure operation flow of HAMA BSP Model
- Parallel通过XCM与Moonbeam集成,将PARA以及DeFi用例带入Moonbeam生态
- Source code analysis of spark cache
猜你喜欢
![[fpga+pwm] design and implementation of phase shift trigger circuit for three-phase PWM rectifier based on FPGA](/img/ad/c039932abe409d696380e8427173c0.png)
[fpga+pwm] design and implementation of phase shift trigger circuit for three-phase PWM rectifier based on FPGA

Seeing the big from the small: a simple example of domain modeling, understanding "domain driven".

Noah fortune plans to land on the Hong Kong Stock Exchange: the performance fell sharply in the first quarter, and once stepped on the thunder "Chengxing case"

Gridhome, a must-have static site generator for beginners

Quartus prime 18.0 software installation package and installation tutorial

Description of new features and changes in ABP Framework version 5.3.0

Hello Playwright:(7)模拟键盘和鼠标

网传学习通1.7亿密码泄露!有什么补救措施?

【阿里云服务器-安装mysql的5.6版本安装,重装】

Huawei cloud recruits partners in the field of industrial intelligence to provide strong support + commercial realization
随机推荐
NLog自定义Target之MQTT
Figure operation flow of HAMA BSP Model
System throughput, TPS (QPS), user concurrency, performance test concepts and formulas
Principle of synchronized implementation
What is a flush? Is online account opening safe?
跨平台Brave浏览器
Quartus Prime 18.0软件安装包和安装教程
Post to asp Net core length limitation and solution when transferring data
mysql-5.6.21-centos6.5源码安装配置
be based on. NETCORE development blog project starblog - (12) razor page dynamic compilation
WPF 实现星空效果
AD20/Altium designer——过孔盖油
一文带你掌握Tcpdump命令的使用
企业级软件开发新模式:低代码
推荐7款超级好用的终端工具 —— SSH+FTP
Development mode of JSP learning
团队管理|如何提高技术 Leader 的思考技巧?
Noah fortune plans to land on the Hong Kong Stock Exchange: the performance fell sharply in the first quarter, and once stepped on the thunder "Chengxing case"
You call this crap high availability?
MySQL string field to floating point field