-1

I am working with a data frame in R and I want to replace the values of certain id to NA. Here is an example of the input file.

id  y
222 12
432 23
522 22
443 11

And here is my desired output:

id  y
222 12
432 NA
522 22
443 NA
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
user4665665
  • 29
  • 3
  • 7

1 Answers1

3
idsToBeReplaced <- c(432, 443)    
df[df$id %in% idsToBeReplaced, "y"] <- NA

#  id  y
#1 222 12
#2 432 NA
#3 522 22
#4 443 NA
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213