Andrew Bannerman
Andrew Bannerman

Reputation: 1305

Julia: Within data frame convert missing to 0

In R we can convert NA to 0 with:

df[is.na(df)] <- 0

This works for single columns:

df[ismissing.(df[:col]), :col] = 0

There a way for the full df?

Upvotes: 2

Views: 1167

Answers (2)

Antonello
Antonello

Reputation: 6431

Maybe a simpler way to convert all missing values in a DataFrame is to just use list comprehension:

[df[ismissing.(df[i]), i] = 0 for i in names(df)]

Upvotes: 1

niczky12
niczky12

Reputation: 5063

I don't think there's such a function in DataFrames.jl yet. But you can hack your way around it by combining colwise and recode. I'm also providing a reproducible example here, in case someone wants to iterate on this answer:

julia> using DataFrames

julia> df = DataFrame(a = [missing, 5, 5],
           b = [1, missing, missing])
3×2 DataFrames.DataFrame
│ Row │ a       │ b       │
├─────┼─────────┼─────────┤
│ 1   │ missing │ 1       │
│ 2   │ 5       │ missing │
│ 3   │ 5       │ missing │

julia> DataFrame(colwise(col -> recode(col, missing=>0), df), names(df))
3×2 DataFrames.DataFrame
│ Row │ a │ b │
├─────┼───┼───┤
│ 1   │ 0 │ 1 │
│ 2   │ 5 │ 0 │
│ 3   │ 5 │ 0 │

This is a bit ugly as you have to reassign the dataframe column names.

Upvotes: 3

Related Questions