Home > OS >  How to replace all 0 values to NA but only in a part of data frame?
How to replace all 0 values to NA but only in a part of data frame?

Time:01-27

I want to replace all 0 values to NA but only in subset of columns.

df <- data.frame(replicate(100,sample(0:9,1000,rep=TRUE)))

To change all 0 to NA in the entire df one should use

df[df == 0] <- NA

but I want to change 0 to NA only in subset of columns 5:100, I have tried

df[df == 0][ , 5:100] <- NA
df[df == 0][5:100] <- NA
df[ , 5:100][df == 0] <- NA
df[5:100][df == 0] <- NA
df[5:100][df[5:1000] == 0] <- NA
df[df[5:1000] == 0] <- NA
df[which(df[, 5:100] == 0)] <- NA
df[which(df[5:100] == 0)] <- NA

but all of that returns an error.

How to apply the operation to part of the df?

CodePudding user response:

You should do the subsetting on both df calls:

df[5:100][df[5:100] == 0] <- NA

Takes this smaller example:

set.seed(1)
df <- data.frame(replicate(3,sample(0:9,10,rep=TRUE)))
df[2:3][df[2:3] == 0] <- NA

   X1 X2 X3
1   8  4  4
2   3  9  4
3   6  5  1
4   0  9  9
5   1  6  8
6   6  8 NA
7   1  4  3
8   2  4  2
9   0  8  5
10  4  8  9

CodePudding user response:

dplyr option using across and replace like this:

df <- data.frame(replicate(100,sample(0:9,1000,rep=TRUE)))

library(dplyr)
df %>%
  mutate(across(5:100, ~ replace(.x, .x == 0, NA)))
  • Related