Replace missing values with zeros and nonmissing values with one in Dataframe


I would like to recode a dataframe containing positive numbers and missings such that every positive number is replaced with one and every missing is replaced with zero.

I found some similar solutions but was unable to adjust them to Dataframes.

# Input 
df = DataFrame(A = [1, missing,3, 4], B = [5,6,missing,7])

# Desired Output 
nonmissing = DataFrame(B = [1, 0, 1, 1], C = [1,1,0,1])
nonmissing = .!ismissing.(df)
1 Like