How to replace NaN value with zero in a huge data frame?
In fact, in R, this operation is very easy:
If the matrix 'a' contains some NaN, you just need to use the following code to replace it by 0:
a <- matrix(c(1, NaN, 2, NaN), ncol=2, nrow=2)a[is.nan(a)] <- 0a
If the data frame 'b' contains some NaN, you just need to use the following code to replace it by 0:
#for a data.frame: b <- data.frame(c1=c(1, NaN, 2), c2=c(NaN, 2, 7))b[is.na(b)] <- 0b
Note the difference is.nan
when it's a matrix vs. is.na
when it's a data frame.
Doing
#...b[is.nan(b)] <- 0#...
yields: Error in is.nan(b) : default method not implemented for type 'list'
because b is a data frame.
Note: Edited for small but confusing typos