5 fill()

When reporting the gender distribution of a school to U.S. state education agencies, for example, you may have missing student data on gender but are still required to produce the full count of students. The fill() function can be useful in this regard, as it will fill in missing values while maintaining the existing proportions of the unique values (excluding NA values).

# Always set the seed before using randomizing functions.
set.seed(1) 

# Our vector to fill in.
k <- c(0, 1, NA, 1, 0, NA, NA, NA)

# Original proportions
prop_original <- prop.table(table(k)) 

prop_original
## k
##   0   1 
## 0.5 0.5
# Apply fill() function
fill_k <- fill(k) # A warning may occur

# Compare original vector to the new vector.
cbind(k, fill_k) 
##       k fill_k
## [1,]  0      0
## [2,]  1      1
## [3,] NA      1
## [4,]  1      1
## [5,]  0      0
## [6,] NA      1
## [7,] NA      0
## [8,] NA      0
# Check if proportions were maintained
prop_new <- prop.table(table(fill_k)) 

prop_new
## fill_k
##   0   1 
## 0.5 0.5
all(prop_original == prop_new)
## [1] TRUE