0

I want to clean my data in one go, but the following code doesn't work. Data has factor features and numeric features as well.

findnullna <- function(x) {
  for(i in 1:length(x)) {
    if(x[i]=="" | x[i]=="NULL" | x[i]=="#N/A" | x[i]=="#NAME?") {
      x[i] <- NA
    }
  }
  return(x)
}


df <- sapply(df,findnullna)

I get an error

Error in if (x[i] == "" | x[i] == "NULL" | x[i] == "#N/A" | x[i] == "#NAME?") { : missing value where TRUE/FALSE needed

also sometimes I get

Error in x[i] : object of type 'symbol' is not subsettable

jogo
  • 12,469
  • 11
  • 37
  • 42
user245204
  • 23
  • 1
  • 4
  • 1
    Could you please 'dput()' your data? rep reducible would help all of us :) https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – DJV Jun 05 '18 at 08:28
  • 6
    You should use the `na.strings` argument of `read.table` (or whatever function you use for import). – Roland Jun 05 '18 at 08:31
  • 3
    Aside from any incompatibilities in your data (which we can only guess unless you provide a reasonable example), you should use `ifelse(x %in% c("", "NULL", ...), NA, x)` instead of your `for`-loop solution. – LAP Jun 05 '18 at 08:54

0 Answers0