Search code examples
rdplyrna

in dplyr , a question about `na_if(x,y)` when values y equals 0 or 1


in dplyr , na_if can transfer value to NA, but when I want to change 0 or 1 to NA failed . How fix it ?

library(dplyr)
x <- c(1, -1, 0, 10)

this is ok

na_if(x,0)

but below code show Can't recycle y (size 2) to size 4. I am wondering 0 and c(0,1) are not size 4 , but na_if(x,0) can work.

na_if(x,c(0,1))

Solution

  • Not really a solution, but an explanation as to why this is happening.

    na_if does not allow to replace several values with NA (see also here).

    Check the documentation:

    y

    Value or vector to compare against. When x and y are equal, the value in x will be replaced with NA.

    y is cast to the type of x before comparison.

    y is recycled to the size of x before comparison. This means that y can be a vector with the same size as x, but most of the time this will be a single value.

    Emphasis mine. The second argument has to be either of size 1 or size length(x).