Replace all values in a matrix <0.1 with 0

后端 未结 6 1241
执念已碎
执念已碎 2020-11-29 23:31

I have a matrix (2601 by 58) of particulate matter concentration estimates from an air quality model. Because real-life air quality monitors cannot measure below 0.1 ug/L,

6条回答
  •  被撕碎了的回忆
    2020-11-30 00:31

    Further equivalent methods:

    let:

    M=matrix(rnorm(10*10), 10, 10)
    

    Brute force (educative)

    for (i in 1:nrow(M)) {
        for (j in 1:ncol(M)) if (M[i,j]<0.1 & !is.na(M[i,j]) ) M[i,j]=NA
        }
    

    If there are missing values (NA) in M, omitting !is.na will give errors.

    Another way: using recode in package car:

    library(car)
    recode(M, "lo:0.099999=NA")
    

    Can't specify a strict inequality here, so that's why there's a bunch of 9. Put more nines and it turns into 0.1. lo is a convenience of recode, which gives the minimum value (removing NAs).

提交回复
热议问题