Remove NA/NaN/Inf in a matrix

后端 未结 4 2113
轮回少年
轮回少年 2020-12-06 00:44

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

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-06 01:08

    NaRV.omit(x) is my preferred option for question 1. Mnemonic NaRV means "not a regular value".

    require(IDPmisc)
    m <- matrix(c(1,2,3,NA,5, NaN, 7, 8, 9, Inf, 11, 12, -Inf, 14, 15), 5)
    > m
         [,1] [,2] [,3]
    [1,]    1  NaN   11
    [2,]    2    7   12
    [3,]    3    8 -Inf
    [4,]   NA    9   14
    [5,]    5  Inf   15
    > NaRV.omit(m)
         [,1] [,2] [,3]
    [1,]    2    7   12
    attr(,"na.action")
    [1] 1 3 4 5
    attr(,"class")
    [1] "omit"
    

提交回复
热议问题