Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace all values in a matrix <0.1 with 0

Tags:

replace

r

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, I need to replace all values in my matrix that are <0.1 with a zero/NA/null value.

Someone suggested ifelse(test, true, false) with a logical statement, but when I try this it deletes everything.

like image 440
mEvans Avatar asked Feb 24 '12 23:02

mEvans


People also ask

How do you change a value to 0 in R?

Replacing values in a data frame is a very handy option available in R for data analysis. Using replace() in R, you can switch NA, 0, and negative values with appropriate to clear up large datasets for analysis.

How do I replace NAs with 0 in R?

To replace NA with 0 in an R data frame, use is.na() function and then select all those values with NA and assign them to 0. myDataframe is the data frame in which you would like replace all NAs with 0.

How do you create a matrix of 0?

X = zeros( sz ) returns an array of zeros where size vector sz defines size(X) . For example, zeros([2 3]) returns a 2-by-3 matrix. X = zeros(___, typename ) returns an array of zeros of data type typename . For example, zeros('int8') returns a scalar, 8-bit integer 0 .


3 Answers

X[X < .1] <- 0 

(or NA, although 0 sounds more appropriate in this case.)

Matrices are just vectors with dimensions, so you can treat them like a vector when you assign to them. In this case, you're creating a boolean vector over X that indicates the small values, and it assigns the right-hand-side to each element that's TRUE.

like image 83
Harlan Avatar answered Sep 18 '22 22:09

Harlan


ifelse should work:

mat <- matrix(runif(100),ncol=5) mat <- ifelse(mat<0.1,NA,mat) 

But I would choose Harlan's answer over mine.

mat[mat < 0.1] <- NA 
like image 41
Justin Avatar answered Sep 19 '22 22:09

Justin


I think you will find that 'ifelse' is not a vector operation (its actually performing as a loop), and so it is orders of magnitudes slower than the vector equivalent. R favors vector operations, which is why apply, mapply, sapply are lightning fast for certain calculations.

Small Datasets, not a problem, but if you have an array of length 100k or more, you can go and cook a roast dinner before it finishes under any method involving a loop.

The below code should work.

For vector

minvalue <- 0
X[X < minvalue] <- minvalue

For Dataframe or Matrix.

minvalue <- 0
n <- 10 #change to whatever.
columns <- c(1:n)
X[X[,columns] < minvalue,columns] <- minvalue

Another fast method, via pmax and pmin functions, this caps entries between 0 and 1 and you can put a matrix or dataframe as the first argument no problems.

ulbound <- function(v,MAX=1,MIN=0) pmin(MAX,pmax(MIN,v))
like image 42
ADP Avatar answered Sep 21 '22 22:09

ADP