I have input data from SQL which has fields that are the string “NULL”, such that
unique(file$x)
returns
Levels: 1 10 14 8 NULL
I would like any of the results that are “NULL” to become “0” and then to convert using as.numeric().
I have tried:
i <- 1
for(i <= nrow(file) {
if(file$x[i] == "NULL") {
file$x[i] <- 0
}
i <- i + 1
}
However, I get the result now that the NULL has simply turned into and I am still unable to convert it to the number 0.
If you’re reading this in with
read.csv, set the optionna.strings='NULL'. This will importfile$xasnumeric, instead offactor, with numericNAs objects in place of theNULLstrings. Then to replaceNAs with0s: