On this page:
replace-na
drop-na

13 Missing values🔗ℹ

Occasionally, a data-set will have a literal missing value. While reading data into a data-frame, this can be specified with df-read/csv’s #:na argument; but usually, this value ends up being #f.

These operations are designed to handle missing values, either by replacing them or dropping them.

The following example data-frame will be used in this section:
> (define has-some-na
    (row-df [col-a col-b]
            1      #f
            #f     2
            3      4))

procedure

(replace-na df column-name replace-with ...)

  (or/c data-frame? grouped-data-frame?)
  df : (or/c data-frame? grouped-data-frame?)
  column-name : string?
  replace-with : any/c
Returns a new data-frame with NA values in df replaced with another value. In each column-name, NA is replaced with replace-with.

Examples:
> (~> has-some-na
      (replace-na "col-a" 999)
      show)

data-frame: 3 rows x 2 columns

┌─────┬─────┐

│col-b│col-a│

├─────┼─────┤

│#f   │1    

├─────┼─────┤

│2    │999  

├─────┼─────┤

│4    │3    

└─────┴─────┘

> (~> has-some-na
      (replace-na "col-a" 999
                  "col-b" 333)
      show)

data-frame: 3 rows x 2 columns

┌─────┬─────┐

│col-a│col-b│

├─────┼─────┤

│1    │333  

├─────┼─────┤

│999  │2    

├─────┼─────┤

│3    │4    

└─────┴─────┘

syntax

(drop-na df slice-spec)

 
  df : (or/c data-frame? grouped-data-frame?)
Returns a new data-frame like df, except with NA values removed from columns specified by the evaluation of slice-spec.

The evaluation of slice-spec determines what columns to remove NA values from. For documentation on this language, see Slicing.

Examples:
> (~> has-some-na
      (drop-na everything)
      show)

data-frame: 1 rows x 2 columns

┌─────┬─────┐

│col-a│col-b│

├─────┼─────┤

│3    │4    

└─────┴─────┘

> (~> has-some-na
      (drop-na "col-a")
      show)

data-frame: 2 rows x 2 columns

┌─────┬─────┐

│col-a│col-b│

├─────┼─────┤

│1    │#f   

├─────┼─────┤

│3    │4    

└─────┴─────┘