How to correctly convert NULL variables to 0 in an R table

23.5k Views Asked by At

I have a table that for some variables has missing data (recorded as NULL) - I'd like to convert some of these missing cells to hold a 0 but for some reason I can't seem to get the syntax correct. My initial approach was to do this:

b<- eval(parse(text=paste(table_full$','column_name1',sep='')))
b[which(is.na(b))]<-0
b[which(b=='NULL')]<-0

and then save the data to a file, however - this still results in missing data in the output files and warning messages like:

In `[<-.factor`(`*tmp*`, which(is.na(b)), value = 0) :
  invalid factor level, NA generated

Alternatively, I've tried things of the form:

b[which(is.na(as.numeric(as.character(b))))]<-0

but this didn't resolve the situation.

I'm relatively new to R and can't understand exactly what I'm doing wrong here. Thanks in advance!

3

There are 3 best solutions below

1
On

is.na() returns TRUE or FALSE. Try b[which(is.na(b) == T)]<-0 instead

1
On

Since R tends not to store its values as "NULL", I'm going to go out on a limb and assume you imported it as text, more specifically as factors. Try reimporting w stringsAsFactors = FALSE and then use your code:

b[b=='NULL'] <- 0

A more elegant way would be to use the na.strings=c("NULL") when you read the data in.

0
On

This will work easily for a column in a data frame:

df$column = ifelse(is.null(df$column), 0, df$columns)

I am using 0, but you can do anything for strings. You can do it with dates, but you need a legitimate data format,