Replace All Values in a Matrix <0.1 with 0

Replace all values in a matrix 0.1 with 0

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

Replace all 0 values to NA

Replacing all zeroes to NA:

df[df == 0] <- NA



Explanation

1. It is not NULL what you should want to replace zeroes with. As it says in ?'NULL',

NULL represents the null object in R

which is unique and, I guess, can be seen as the most uninformative and empty object.1 Then it becomes not so surprising that

data.frame(x = c(1, NULL, 2))
# x
# 1 1
# 2 2

That is, R does not reserve any space for this null object.2 Meanwhile, looking at ?'NA' we see that

NA is a logical constant of length 1 which contains a missing value
indicator. NA can be coerced to any other vector type except raw.

Importantly, NA is of length 1 so that R reserves some space for it. E.g.,

data.frame(x = c(1, NA, 2))
# x
# 1 1
# 2 NA
# 3 2

Also, the data frame structure requires all the columns to have the same number of elements so that there can be no "holes" (i.e., NULL values).

Now you could replace zeroes by NULL in a data frame in the sense of completely removing all the rows containing at least one zero. When using, e.g., var, cov, or cor, that is actually equivalent to first replacing zeroes with NA and setting the value of use as "complete.obs". Typically, however, this is unsatisfactory as it leads to extra information loss.

2. Instead of running some sort of loop, in the solution I use df == 0 vectorization. df == 0 returns (try it) a matrix of the same size as df, with the entries TRUE and FALSE. Further, we are also allowed to pass this matrix to the subsetting [...] (see ?'['). Lastly, while the result of df[df == 0] is perfectly intuitive, it may seem strange that df[df == 0] <- NA gives the desired effect. The assignment operator <- is indeed not always so smart and does not work in this way with some other objects, but it does so with data frames; see ?'<-'.


1 The empty set in the set theory feels somehow related.

2 Another similarity with the set theory: the empty set is a subset of every set, but we do not reserve any space for it.

Replace values greater than zero with 1 in r

The following will convert all non-zero numeric values to 1:

df.richness %>% mutate_if(is.numeric, ~1 * (. != 0))

while

df.richness %>% mutate_if(is.numeric, ~1 * (. > 0))

will do that with those greater than zero.

Python: How to replace negative values with 0 in a matrix?

Easiest solution I would suggest is using numpy for handling any matrix operations so you won't have to reimplement the wheel. Maybe it would be worth looking into numpy masking operations, like masked_where

I'm not saying this is the most efficient solution, really don't know what you consider big matrix and what sort of efficiency you need, I would probably try several approaches, and compare them using profiler and decide which is best for you.

>>> import numpy
>>> x = numpy.array([1, 2, 3, -99, 5])
>>> c = numpy.ma.masked_where(x < 0, x, copy=False)
>>> c
masked_array(data = [1 2 3 -- 5],
mask = [False False False True False],
fill_value = 999999)

>>> x2 = c.filled(0)
>>> x2
array([1, 2, 3, 0, 5])

How can I replace specific values within a large matrix with NA's in R

Try this:

m = matrix(sample(c(-9999.0,1:10), 10000, replace=T), ncol=100)

m[m==-9999.0] = NA

Replace zeros in matrix in R with element from row below


df <- data.frame(
sc_start = c(227,0,272,298,0,353,0,0,0,0,0,0,0),
sc_end = c(0,235,279,0,306,0,0,0,0,0,0,0,387)
)
df2 <- data.frame(
sc_start = c(df$sc_start[!df$sc_start==0]),
sc_end = c(df$sc_end[!df$sc_end==0])
)

This works only if, removing zeros, the length of both series is equal.



Related Topics



Leave a reply



Submit