Way to check if value is NA and replace it

Hello,

I am trying to replace all ‘NA’ values with 0 in MatrixTable.
I found in a previous post that the only way to evaluate NA is hl.is_defined()

However here:

mt.annotate_entries(DP = mt.DP if hl.is_defined(mt.DP) else 0)

it ends with error:

TypeError: ‘Expression’ objects have no static length: use ‘hl.len’ for the length of collections

I tries also

mt.annotate_entries(DP = mt.DP if mt.DP == hl.null(‘int’) else 0)

, but it returns again ‘NA’ value

Any suggestions?

Cheers,

You can’t do a normal if else the way you are. It’s a hail BooleanExpression, not a regular boolean. You’d have to do hl.if_else(hl.is_defined(mt.DP), mt.DP, 0)

That’s still not the easiest way to do this. If you look on bottom left of https://hail.is/docs/0.2/_static/cheatsheets/hail_tables_cheat_sheet.pdf , you’ll see “handling missing data”. You probably want:

mt.annotate_entries(DP = hl.coalesce(mt.DP, 0))

wow thank you, I totally missed the table cheat_sheet! I was struggling only with the matrix table one :smiley:

You’re welcome. The “Table” one actually has lots of general info on it, like handling missing data. The “MatrixTable” one mostly just shows how things generalize to MatrixTables (i.e. annotate on Tables has corresponding annotate_rows, annotate_columns, and annotate_entries on MatrixTables).

1 Like