1

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?

1
  • I had that but with a mistake (put 1000 instead of 100) Commented Jan 27, 2023 at 12:17

2 Answers 2

2

You should do the subsetting on both df calls:

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

Take 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
Sign up to request clarification or add additional context in comments.

Comments

0

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)))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.