Remove NA/NaN/Inf in a matrix

后端 未结 4 2114
轮回少年
轮回少年 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:09

    Just another way (for the first question):

    m <- structure(c(1, 2, 3, NA, 4, 5, Inf, 5, 6, NaN, 7, 8), 
                  .Dim = c(4L, 3L))
    #      [,1] [,2] [,3]
    # [1,]    1    4    6
    # [2,]    2    5  NaN
    # [3,]    3  Inf    7
    # [4,]   NA    5    8
    
    m[complete.cases(m * 0), , drop=FALSE]
    #      [,1] [,2] [,3]
    # [1,]    1    4    6
    

    I can't think anything else other than Matthew's answer for the second part.

提交回复
热议问题