Summing over missing column values in a DataFrame

Hi, I’m using DataFramesMeta and having trouble with wrangling with missing values.

df = DataFrame(x = 1:5, y = [missing, 4, missing, 2, 1])

x y
Int64 Int64?
1 1 missing
2 2 4
3 3 missing
4 4 2
5 5 1

I want to create a new column that sums over x and y.

df2 = @transform(df, z = sum(skipmissing([:x, :y])))

I thought the skipmissing feature would help out here, but struggling to make it work. Any tips?

Do you need to do this by row? Like add :x and :y from each row?

On more recent versions, you can do

@transform df @byrow y = sum(skipmissing([:x, :y]))
1 Like

what’s your expected output?

using Chain, DataFrameMacros
@chain df begin
  @transform :z = @m :x + :y

@chain df begin
  @transform :z = sum(skipmissing([:x, :y]))