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

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

问题描述

我想尝试两件事:

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

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

>例如 <- 数据[rowSums(is.na(data)) == 0,]

<块引用>

 rowSums(is.na(data)) 中的错误:'x' 必须是至少有两个维度的数组另外: 警告信息:在 is.na(data) 中: is.na() 应用于闭包"类型的非(列表或向量)

解决方案

我想我会用我喜欢的方法把我的帽子扔进戒指:

# 示例数据m <- 矩阵(c(1,2,NA,NaN,1,Inf,-1,1,9,3),5)# 删除所有具有非有限值的行m[!rowSums(!is.finite(m)),]# 用0替换所有非有限值m[!is.finite(m)] <- 0

I want to try two things :

  1. How do I remove rows that contain NA/NaN/Inf
  2. How do I set value of data point from NA/NaN/Inf to 0.

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天全站免登陆