Search code examples
rfor-loopmissing-data

trying to turn nonsense data records in each column to NULL using for loop in R using R studio, but showed error: replacement has length zero


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


Solution

  • 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.