在矩阵中删除NA/NaN/Inf [英] Remove NA/NaN/Inf in a matrix

查看:376
本文介绍了在矩阵中删除NA/NaN/Inf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试两件事:

  1. 如何删除包含NA/NaN/Inf的行
  2. 如何将数据点的值从NA/NaN/Inf设置为0.

到目前为止,我已经尝试将以下值用于NA值,但一直收到警告.

So far, I have tried using the following for NA values, but been getting warnings.

> eg <- data[rowSums(is.na(data)) == 0,]

 Error in rowSums(is.na(data)) : 
       'x' must be an array of at least two dimensions
     In addition: Warning message:
     In is.na(data) : is.na() applied to non-(list or vector) of type 'closure'

推荐答案

我想我会用自己喜欢的方法戴上帽子:

I guess I'll throw my hat into the ring with my preferred methods:

# sample data
m <- matrix(c(1,2,NA,NaN,1,Inf,-1,1,9,3),5)
# remove all rows with non-finite values
m[!rowSums(!is.finite(m)),]
# replace all non-finite values with 0
m[!is.finite(m)] <- 0

这篇关于在矩阵中删除NA/NaN/Inf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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