How to complete missing factor levels in data frame?

Solution 1:

The complete takes the first argument as 'data', followed by the columns to expand. By default, the fill is NA, but we can change it to 0 by specifying it in a list.

complete(df, PERSON, FRUIT, fill = list(A=0, B = 0))