I would like to change all 0's to, say 0.0001, in a list of dataframes to avoid -Inf when take log. So followed the instruction from Replace all 0 values to NA, I wrote my function as
set_zero_as_value <- function(x, value=0.0001){
x[x == 0] <- value
}
However when I use sapply
to my data sapply(a,set_zero_as_value)
, the result returned as
s1 s2
1e-04 1e-04
And further check the list a, the 0 in a
does not change at all. Is there a solution for this?
PS: list a
can be created as
> a = NULL
> a$s1 = rbind(cbind(0,1,2),cbind(3,4,5))
> a$s2 = rbind(cbind(0,1,2),cbind(3,4,5))