-1

i have a dataframe that looks like this :

Date = seq(as.Date("2022/1/1"), by = "day", length.out = 10)
x = c(rnorm(9),NA)
y = c(NA,0,rnorm(4),6,0,0,10)
z = c(rnorm(4),0,0,NA,NA,3,2)
d = tibble(Date,x,y,z);d
# A tibble: 10 x 4
   Date                 x          y          z
   <date>           <dbl>      <dbl>      <dbl>
 1 2022-01-01  2.456174   NA          0.2963012
 2 2022-01-02  0.3648335   0          0.3981664
 3 2022-01-03  0.8283570  -0.1843364  1.194378 
 4 2022-01-04  1.061199    1.507231  -0.2337116
 5 2022-01-05 -0.07824196 -0.6708553  0        
 6 2022-01-06 -0.2654019   0.3008499  0        
 7 2022-01-07  1.426953    6         NA        
 8 2022-01-08 -0.5776817   0         NA        
 9 2022-01-09  0.8706953   0          3        
10 2022-01-10 NA          10          2   

how i can replace all the zeros across all columns with NA using Dplyr package ?

Homer Jay Simpson
  • 1,043
  • 6
  • 19

3 Answers3

2

I would avoid using dplyr here (3 base R example already in comments on OP) but you could

library(dplyr)
d |> mutate_all( \(x) replace(x, x == 0, NA))

#         x      y       z
#     <dbl>  <dbl>   <dbl>
#  1 -0.626 NA     -2.21  
#  2  0.184 NA      1.12  
#  3 -0.836 -0.305 -0.0449
#  4  1.60   1.51  -0.0162
#  5  0.330  0.390 NA     
#  6 -0.820 -0.621 NA     
#  7  0.487  6     NA     
#  8  0.738 NA     NA     
#  9  0.576 NA      3     
# 10 NA     10      2   

Reproducible data:

set.seed(1)
x = c(rnorm(9),NA)
y = c(NA,0,rnorm(4),6,0,0,10)
z = c(rnorm(4),0,0,NA,NA,3,2)
d = tibble(x,y,z)
s_baldur
  • 29,441
  • 4
  • 36
  • 69
2

With dplyr, you could use na_if():

library(dplyr)

d %>%
  mutate(across(everything(), na_if, 0))

or simply

d[d == 0] <- NA

# Pipeline
d %>%
  `[<-`(d == 0, value = NA)
Darren Tsai
  • 32,117
  • 5
  • 21
  • 51
1

Here is my comment to the question as a dplyr pipe.

suppressPackageStartupMessages(library(dplyr))

x = c(rnorm(9),NA)
y = c(NA,0,rnorm(4),6,0,0,10)
z = c(rnorm(4),0,0,NA,NA,3,2)
Date <- seq(as.Date("2022-01-01"), by = "day", length = length(x))
d = tibble(Date,x,y,z)

d %>%
  mutate(across(everything(), ~`is.na<-`(., . == 0)))
#> # A tibble: 10 × 4
#>    Date             x      y      z
#>    <date>       <dbl>  <dbl>  <dbl>
#>  1 2022-01-01 -0.311  NA     -0.891
#>  2 2022-01-02 -0.192  NA      0.278
#>  3 2022-01-03  1.24    0.742 -0.331
#>  4 2022-01-04  0.0130 -1.18   0.384
#>  5 2022-01-05 -1.11   -1.17  NA    
#>  6 2022-01-06 -0.330  -0.629 NA    
#>  7 2022-01-07 -1.25    6     NA    
#>  8 2022-01-08  0.0937 NA     NA    
#>  9 2022-01-09  0.986  NA      3    
#> 10 2022-01-10 NA      10      2

Created on 2022-09-14 with reprex v2.0.2

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66