0

I was trying to turn nonsense data to NA. (ex: wage column that has -4, -3. poverty ratio column has -4,-5, character type column has numbers such as -4, -5)

I used a predefined vector to define which are nonsense data:

nulldata <- c(-5, -4, -3, -2, -1, '')

then, I created a for loop to iterate thru each column to check if there are data like those in the column. the for loop I created:

for(i in 1: ncol(df)){
  df[, i][df[, i] %in% nulldata] <- NULL
}

the error I get is:

Error in df[, i][df[, i] %in% nulldata] <- NULL : 
  replacement has length zero

not sure does anybody else getting the same error

  • Welcome to Stack Overflow. You need to [make this question reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by including plain text example data from the data frame `df` using _e.g._ `dput(df)` if that is not too large. – neilfws Oct 16 '22 at 23:20

1 Answers1

0

Try:

for(i in 1: ncol(df)){
   df[,i] <- ifelse(df[,i] %in% nulldata, NA, df[,i])}

or

for(i in 1: ncol(df)){
   df[df[,i] %in% nulldata, i] <- NA}

NULL does not set values to NA. You can set a column to NULL to delete it, for example.

Ric
  • 5,362
  • 1
  • 10
  • 23