Data Frame
Overview
A common lisp data frame is a collection of observations of sample variables that shares many of the properties of arrays and lists. By design it can be manipulated using the same mechanisms used to manipulate lisp arrays. This allow you to, for example, transform a data frame into an array and use array-operations to manipulate it, and then turn it into a data frame again to use in modeling or plotting.
Data frame is implemented as a two-dimensional common lisp data structure: a vector of vectors for data, and a hash table mapping variable names to column vectors. All columns are of equal length. This structure provides the flexibility required for column oriented manipulation, as well as speed for large data sets.
Note
In this document we refer to column and variable interchangeably. Likewise factor and category refer to a variable type. Where necessary we distinguish the terminology.Load/install
Data-frame is part of the Lisp-Stat package. It can be used independently if desired. Since the examples in this manual use Lisp-Stat functionality, we’ll use it from there rather than load independently.
Within the Lisp-Stat system, the LS-USER
package is the package for
you to do statistics work. Type the following to change to that
package:
Note
The examples assume that you are in package LS-USER. You should make a habit of always working from theLS-USER
package. All the samples may be copied to the clipboard using the
copy
button in the upper-right corner of the sample code
box.
Naming conventions
Lisp-Stat has a few naming conventions you should be aware of. If you see a punctuation mark or the letter ‘p’ as the last letter of a function name, it indicates something about the function:
- ‘!’ indicates that the function is destructive. It will modify the data that you pass to it. Otherwise, it will return a copy that you will need to save in a variable.
- ‘p’, ‘-p’ or ‘?’ means the function is a predicate, that is returns a Boolean truth value.
Data frame environment
Although you can work with data frames bound to symbols (as would
happen if you used (defparameter ...)
, it is more convenient to
define them as part of an environment. When you do this, the system
defines a package of the same name as the data frame, and provides a
symbol for each variable. Let’s see how things work without an
environment:
First, we define a data frame as a parameter:
Now if we want a column, we can say:
Now let’s define an environment using defdf
:
Now we can access the same variable with:
defdf
does a lot more than this, and you should probably use defdf
to set up an environment instead of defparameter
. We mention it here because there’s an important bit about maintaining the environment to be aware of:
Note
Destructive functions (those ending in ‘!’), will automatically update the environment for you. Functions that return a copy of the data will not.defdf
The defdf
macro is conceptually equivalent to the Common
Lisp defparameter
, but with some additional functionality that makes
working with data frames easier. You use it the same way you’d use
defparameter
, for example:
We’ll use both ways of defining data frames in this manual. The access
methods that are defined by defdf
are described in the
access data section.
Data types
It is important to note that there are two ’types’ in Lisp-Stat: the
implementation type and the ‘statistical’ type. Sometimes these are
the same, such as in the case of reals
; in other situations they are
not. A good example of this can be seen in the mtcars
data set. The
hp
(horsepower), gear
and carb
are all of type integer
from an
implementation perspective. However only horsepower
is a continuous
variable. You can have an additional 0.5 horsepower, but you cannot
add an additional 0.5 gears or carburetors.
Data types are one kind of property that can be set on a variable.
As part of the recoding and data cleansing process, you will want to add
properties to your variables. In Common Lisp, these are plists
that
reside on the variable symbols, e.g. mtcars:mpg
. In R they are
known as attributes
. By default, there are three properties for
each variable: type, unit and label (documentation). When you load
from external formats, like CSV, these properties are all nil
; when
you load from a lisp file, they will have been saved along with the
data (if you set them).
There are seven data types in Lisp-Stat:
- string
- integer
- double-float
- single-float
- categorical (
factor
in R) - temporal
- bit (Boolean)
Numeric
Numeric types, double-float
, single-float
and integer
are all
essentially similar. The vector versions have type definitions (from
the numeric-utilities package) of:
- simple-double-float-vector
- simple-single-float-vector
- simple-fixnum-vector
As an example, let’s look at mtcars:mpg
, where we have a variable of
type float, but a few integer values mixed in.
The values may be equivalent, but the types are not. The CSV
loader has no way of knowing, so loads the column as a mixture of
integers and floats. Let’s start by reloading mtcars
from the CSV
file:
and look at the mpg
variable:
Notice that the first two entries in the vector are integers, and the
remainder floats. To fix this manually, you will need to coerce each
element of the column to type double-float
(you could use
single-float
in this case; as a matter of habit we usually use
double-float
) and then change the type of the vector to a
specialised float
vector.
You can use the heuristicate-types
function to guess the statistical
types for you. For reals
and strings
, heuristicate-types
works
fine, however because integers
and bits
can be used to encode
categorical
or numeric values, you will have to indicate the type
using set-properties
. We see this below with gear
and carb
,
although implemented as integer
, they are actually type
categorical
. The next sections describes how to set them.
Using describe, we can view the
types of all the variables that heuristicate-types
set:
Notice the system correctly typed vs
and am
as Boolean (bit
)
(correct in a mathematical sense)
Strings
Unlike in R, strings are not considered categorical variables by default. Ordering of strings varies according to locale, so it’s not a good idea to rely on the strings. Nevertheless, they do work well if you are working in a single locale.
Categorical
Categorical variables have a fixed and known set of possible values.
In mtcars
, gear
, carb
vs
and am
are categorical variables,
but heuristicate-types
can’t distinguish categorical
types, so
we’ll set them:
Temporal
Dates and times can be surprisingly complicated. To make working with
them simpler, Lisp-Stat uses vectors of
localtime objects to
represent dates & times. You can set a temporal type with
set-properties
as well using the keyword :temporal
.
Units & labels
To add units or labels to the data frame, use the set-properties
function. This function takes a plist of variable/value pairs, so to
set the units and labels:
Now look at the description again:
You can set your own properties with this command too. To make your
custom properties appear in the describe
command and be saved
automatically, override the describe
and write-df
methods, or use
:after
methods.
Create data-frames
A data frame can be created from a Common Lisp array
, alist
,
plist
, individual data vectors, another data frame or a vector-of
vectors. In this section we’ll describe creating a data frame from each of these.
Data frame columns represent sample set variables, and its rows are observations (or cases).
Note
For these examples we are going to install a modified version of the Lisp-Stat data-frame print-object function. This will cause the REPL to display the data-frame at creation, and save us from having to type (print-data data-frame) in each example. If you’d like to install it as we have, execute the code below at the REPL.You can ignore the warning that you’ll receive after executing the code above.
Let’s create a simple data frame. First we’ll setup some variables (columns) to represent our sample domain:
Let’s print plist
. Just type the name in at the REPL prompt.
From p/a-lists
Now suppose we want to create a data frame from a plist
We could also have used the plist-df
function:
and to demonstrate the same thing using an alist, we’ll use the
alexandria:plist-alist
function to convert the plist
into an
alist
:
From vectors
You can use make-df
to create a data frame from keys and a list of
vectors. Each vector becomes a column in the data-frame.
This is useful if you’ve started working with variables defined with
defparameter
or defvar
and want to combine them into a data frame.
From arrays
matrix-df
converts a matrix (array) to a data-frame with the given
keys.
This is useful if you need to do a lot of numeric number-crunching on
a data set as an array, perhaps with BLAS or array-operations
then
want to add categorical variables and continue processing as a
data-frame.
Example datasets
Vincent Arel-Bundock maintains a library of over 1700 R
datasets that is a
consolidation of example data from various R packages. You can load
one of these by specifying the url to the raw
data to the read-csv
function. For example to load the
iris
data set, use:
Default datasets
To make the examples and tutorials easier, Lisp-Stat includes the URLs
for the R built in data sets. You can see these by viewing the
rdata:*r-default-datasets*
variable:
To load one of these, you can use the name of the data set. For example to load mtcars
:
If you want to load all of the default R data sets, use the
rdata:load-r-default-datasets
command. All the data sets included in
base R will now be loaded into your environment. This is useful if you
are following a R tutorial, but using Lisp-Stat for the analysis
software.
You may also want to save the default R data sets in order to augment
the data with labels, units, types, etc. To save all of the default R
data sets to the LS:DATA;R
directory, use the
(rdata:save-r-default-datasets)
command if the default data sets
have already been loaded, or save-r-data
if they have not. This
saves the data in lisp format.
Install R datasets
To work with all of the R data sets, we recommend you use git
to
download the repository to your hard drive. For example I downloaded the
example data to the s:
drive like this:
and setup a logical host in my ls-init.lisp
file like so:
Now you can access any of the datasets using the logical
pathname. Here’s an example of creating a data frame using the
ggplot
mpg
data set:
Searching the examples
With so many data sets, it’s helpful to load the index into a data
frame so you can search for specific examples. You can do this by
loading the rdata:index
into a data frame:
I find it easiest to use the SQL-DF system to query this data. For example if you wanted to find the data sets with the largest number of observations:
Export data frames
These next few functions are the reverse of the ones above used to create them. These are useful when you want to use foreign libraries or common lisp functions to process the data.
For this section of the manual, we are going to work with a subset of
the mtcars
data set from above. We’ll use the
select package to take the first 5 rows so that
the data transformations are easier to see.
The next three functions convert a data-frame to and from standard
common lisp data structures. This is useful if you’ve got data in
Common Lisp format and want to work with it in a data frame, or if
you’ve got a data frame and want to apply Common Lisp operators on it
that don’t exist in df
.
as-alist
Just like it says on the tin, as-alist
takes a data frame and
returns an alist
version of it (formatted here for clearer output –
a pretty printer that outputs an alist in this format would be a
welcome addition to Lisp-Stat)
as-plist
Similarly, as-plist
will return a plist
:
as-array
as-array
returns the data frame as a row-major two dimensional lisp
array. You’ll want to save the variable names using the
keys function to make it easy to
convert back (see matrix-df). One of the reasons you
might want to use this function is to manipulate the data-frame using
array-operations. This is
particularly useful when you have data frames of all numeric values.
Our abbreviated mtcars
data frame is now a two dimensional Common
Lisp array. It may not look like one because Lisp-Stat will ‘print
pretty’ arrays. You can inspect it with the describe
command to make
sure:
vectors
The columns
function returns the variables of the data frame as a vector of
vectors:
This is a column-major lisp array.
You can also pass a selection to the columns
function to return
specific columns:
The functions in array-operations are
helpful in further dealing with data frames as vectors and arrays. For
example you could convert a data frame to a transposed array by using
aops:combine with the
columns
function:
Load data
There are two functions for loading data. The first data
makes
loading from logical pathnames convenient. The other, read-csv
works with the file system or URLs. Although the name read-csv
implies only CSV (comma separated values), it can actually read with
other delimiters, such as the tab character. See the DFIO API
reference for more information.
The data command
For built in Lisp-Stat data sets, you can load with just the data set
name. For example to load mtcars
:
If you’ve installed the R data
sets, and want to load
the antigua
data set from the daag
package, you could do it like
this:
If the file type is not lisp
(say it’s TSV or CSV), you need to
specify the type
parameter.
From strings
Here is a short demonstration of reading from strings:
dfio
tries to hard to decipher the various number formats sometimes
encountered in CSV files:
From delimited files
We saw above that dfio
can read from strings, so one easy way to
read from a file is to use the uiop
system function
read-file-string
. We can read one of the example data files
included with Lisp-Stat like this:
That example just illustrates reading from a file to a string. In practice you’re better off just reading the file in directly and avoid reading into a string first:
From parquet files
You can use the duckdb system to load data from parquet files:
(ql:quickload :duckdb) ; see duckdb repo for installation instructions
(ddb:query "INSTALL httpfs;" nil) ; loading via http
(ddb:initialize-default-connection)
(defdf yellow-taxis
(let ((q (ddb:query "SELECT * FROM read_parquet('https://d37ci6vzurychx.cloudfront.net/trip-data/yellow_tripdata_2023-01.parquet') LIMIT 10" nil)))
(make-df (mapcar #'dfio:string-to-symbol (alist-keys q))
(alist-values q))))
Now we can find the average fare:
(mean yellow-taxis:fare-amount)
11.120000000000001d0
From URLs
dfio
can also read from Common Lisp
streams.
Stream operations can be network or file based. Here is an example
of how to read the classic Iris data set over the network:
From a database
You can load data from a SQLite table using the
read-table
command. Here’s an example of reading the iris
data frame from a
SQLite table:
Note that sqlite:connect
does not take a logical pathname; use a
system path appropriate for your computer. One reason you might want
to do this is for speed in loading CSV. The CSV loader for SQLite is
10-15 times faster than the fastest Common Lisp CSV parser, and it is
often quicker to load to SQLite first, then load into Lisp.
Save data
Data frames can be saved into any delimited text format supported by fare-csv, or several flavors of JSON, such as Vega-Lite.
As CSV
To save the mtcars
data frame to disk, you could use:
to save it as CSV, or to save it to tab-separated values:
As Lisp
For the most part, you will want to save your data frames as lisp. Doing so is both faster in loading, but more importantly it preserves any variable attributes that may have been given.
To save a data frame, use the save
command:
Note that in this case you are passing the symbol to the function, not the value (thus the quote (’) before the name of the data frame). Also note that the system will add the ’lisp’ suffix for you.
To a database
The write-table function can be used to save a data frame to a SQLite database. Each take a connection to a database, which may be file or memory based, a table name and a data frame. Multiple data frames, with different table names, may be written to a single SQLite file this way.
Access data
This section describes various way to access data variables.
Define a data-frame
Let’s use defdf
to define the iris
data
frame. We’ll use both of these data frames in the examples below.
We now have a global
variable
named iris
that represents the data frame. Let’s look at the first
part of this data:
Notice a couple of things. First, there is a column X29
. In fact if
you look back at previous data frame output in this tutorial you will
notice various columns named X
followed by some number. This is
because the column was not given a name in the data set, so a name was
generated for it. X
starts at 1 and increased by 1 each time an
unnamed variable is encountered during your Lisp-Stat session. The
next time you start Lisp-Stat, numbering will begin from 1 again.
We will see how to clean this up this data frame in the next sections.
The second thing to note is the row numbers on the far left side. When Lisp-Stat prints a data frame it automatically adds row numbers. Row and column numbering in Lisp-Stat start at 0. In R they start with 1. Row numbers make it convenient to select data sections from a data frame, but they are not part of the data and cannot be selected or manipulated themselves. They only appear when a data frame is printed.
Access a variable
The defdf
macro also defines symbol macros that allow you to refer
to a variable by name, for example to refer to the mpg
column of
mtcars, you can refer to it by the the name data-frame:variable
convention.
There is a point of distinction to be made here: the values of mpg
and the column mpg
. For example to obtain the same vector using
the selection/sub-setting package select
we must refer to the
column:
Note that with select
we passed the symbol 'mpg
(you can
tell it’s a symbol because of the quote in front of it).
So, the rule here is: if you want the value refer to it directly,
e.g. mtcars:mpg
. If you are referring to the column, use the
symbol. Data frame operations sometimes require the symbol, where as
Common Lisp and other packages that take vectors use the direct access
form.
Data-frame operations
These functions operate on data-frames as a whole.
copy
copy
returns a newly allocated data-frame with the same values as
the original:
By default only the keys are copied and the original data remains the
same, i.e. a shallow copy. For a deep copy, use the copy-array
function as the key:
Useful when applying destructive operations to the data-frame.
keys
Returns a vector of the variables in the data frame. The keys are symbols. Symbol properties describe the variable, for example units.
Recall the earlier discussion of X1
for the column name.
map-df
map-df
transforms one data-frame into another, row-by-row. Its
function signature is:
(map-df data-frame keys function result-keys) ...
It applies function to each row, and returns a data frame with the
result-keys as the column (variable) names. keys
is a list.
You can also specify the type of the new variables in the
result-keys
list.
The goal for this example is to transform df1
:
into a data-frame that consists of the product of :a
and :b
, and a
bit mask of the columns that indicate where the value is <= 30. First
we’ll need a helper for the bit mask:
Now we can transform df1
into our new data-frame, df2
, with:
Since it was a parameter assignment, we have to view it manually:
Note how we specified both the new key names and their type. Here’s
an example that transforms the units of mtcars
from imperial to metric:
Note that you may have to adjust the X
column name to suit your
current environment.
You might be wondering how we were able to refer to the columns without the ’ (quote); in fact we did, at the beginning of the list. The lisp reader then reads the contents of the list as symbols.
The print-data
command will print a data frame in a nicely formatted
way, respecting the pretty printing row/column length variables:
rows
rows
returns the rows of a data frame as a vector of vectors:
remove duplicates
The df-remove-duplicates
function will remove duplicate rows. Let’s
create a data-frame with duplicates:
Now remove duplicate rows 0 and 1:
remove data-frame
If you are working with large data sets, you may wish to remove a data
frame from your environment to save memory. The undef
command does
this:
LS-USER> (undef 'tooth-growth)
(TOOTH-GROWTH)
You can check that it was removed with the show-data-frames
function, or by viewing the list df::*data-frames*
.
list data-frames
To list the data frames in your environment, use the
show-data-frames
function. Here is an example of what is currently
loaded into the authors environment. The data frames listed may be
different for you, depending on what you have loaded.
To see this output, you’ll have to change to the standard
print-object
method, using this code:
Now, to see all the data frames in your environment:
with the :head t
option, show-data-frames
will print the first
five rows of the data frame, similar to the head
command:
You, of course, may see different output depending on what data frames you currently have loaded.
Let’s change the print-object
back to our convenience method.
Column operations
You have seen some of these functions before, and for completeness we repeat them here.
To obtain a variable (column) from a data frame, use the column
function. Using the mtcars-small
data frame, defined in export data
frames above:
To get all the columns as a vector, use the columns
function:
You can also return a subset of the columns by passing in a selection:
add columns
There are two ‘flavors’ of add functions, destructive and non-destructive. The latter return a new data frame as the result, and the destructive versions modify the data frame passed as a parameter. The destructive versions are denoted with a ‘!’ at the end of the function name.
The columns to be added can be in several formats:
- plist
- alist
- (plist)
- (alist)
- (data-frame)
To add a single column to a data frame, use the add-column!
function. We’ll use a data frame similar to the one used in our
reading data-frames from a string example to illustrate column
operations.
Create the data frame:
and print it:
and add a ‘weight’ column to it:
now that we have weight, let’s add a BMI column to it to demonstrate using a function to compute the new column values:
Now let’s add multiple columns destructively using add-columns!
remove columns
Let’s remove the columns a
and b
that we just added above with
the remove-columns
function. Since it returns a new data frame,
we’ll need to assign the return value to *d*
:
To remove columns destructively, meaning modifying the original data,
use the remove-column!
or remove-columns!
functions.
rename columns
Sometimes data sources can have variable names that we want to change.
To do this, use the rename-column!
function. This example will
rename the ‘gender’ variable to ‘sex’:
If you used defdf
to create your data frame, and this is the
recommended way to define data frames, the variable references within
the data package will have been updated. This is true for all
destructive data frame operations. Let’s use this now to rename the
mtcars
X1
variable to model
. First a quick look at the first 2
rows as they are now:
Replace X1
with model
:
Note: check to see what value your version of mtcars
has. In this
case, with a fresh start of Lisp-Stat, it has X1
. It could have
X2
, X3
, etc.
Now check that it worked:
We can now refer to mtcars:model
replace columns
Columns are “setf-able” places and the simplest way to replace a
column is set the field to a new value. We’ll complement the sex
field of *d*
:
Note that df::setf
is not exported. Use this with caution.
You can also replace a column using two functions specifically for this purpose. Here we’ll replace the ‘age’ column with new values:
That was a non-destructive replacement, and since we didn’t reassign
the value of *d*
, it is unchanged:
We can also use the destructive version to make a permanent change
instead of setf
-ing *d*
:
transform columns
There are two functions for column transformations, replace-column
and map-columns
.
replace-column
replace-column
can be used to transform a column by applying a
function to each value. This example will add 20 to each row of the
age
column:
replace-column!
can also apply functions to a column, destructively
modifying the column.
map-columns
The map-columns
functions can be thought of as applying a function
on all the values of each variable/column as a vector, rather than the
individual rows as replace-column
does. To see this, we’ll use
functions that operate on vectors, in this case nu:e+
, which is the
vector addition function for Lisp-Stat. Let’s see this working first:
observe how the vectors were added element-wise. We’ll demonstrate
map-columns
by adding one to each of the numeric columns in the
example data frame:
recall that we used the non-destructive version of replace-column
above, so *d*
has the original values. Also note the use of select
to get the numeric variables from the data frame; e+
can’t add
categorical values like gender/sex.
Row operations
As the name suggests, row operations operate on each row, or observation, of a data set.
add rows
Adding rows is done with the array-operations stacking functions. Since these functions operate on both arrays and data frames, we can use them to stack data frames, arrays, or a mixture of both, providing they have a rank of 2. Here’s an example of adding a row to the mtcars
data frame:
and now stack it onto the mtcars
data set (load it with (data :mtcars)
if you haven’t already done so):
This is the functional equivalent of R’s rbind
function. You can also add columns with the stack-cols
function.
An often asked question is: why don’t you have a dedicated stack-rows
function? Well, if you want one it might look like this:
But now the data frame must be the first parameter passed to the function. Or perhaps you want to rename the columns? Or you have matrices as your starting point? For all those reasons, it makes more sense to pass in the column keys than a data frame:
However this means we have two stack-rows
functions, and you don’t really gain anything except an extra function call. So use the above definition if you like; we use the first example and call matrix-df
and stack-rows
to stack data frames.
count-rows
This function is used to determine how many rows meet a certain condition. For example if you want to know how many cars have a MPG (miles-per-galleon) rating greater than 20, you could use:
do-rows
do-rows
applies a function on selected variables. The function must
take the same number of arguments as variables supplied. It is
analogous to dotimes, but
iterating over data frame rows. No values are returned; it is purely
for side-effects. Let’s create a new data data-frame to
illustrate row operations:
This example uses format
to illustrate iterating using do-rows
for
side effect:
map-rows
Where map-columns
can be thought of as working through the data
frame column-by-column, map-rows
goes through row-by-row. Here we
add the values in each row of two columns:
Since the length of this vector will always be equal to the data-frame column length, we can add the results to the data frame as a new column. Let’s see this in a real-world pattern, subtracting the mean from a column:
You could also have used replace-column!
in a similar manner to
replace a column with normalize values.
mask-rows
mask-rows
is similar to count-rows
, except it returns a bit-vector
for rows matching the predicate. This is useful when you want to pass
the bit vector to another function, like select
to retrieve only the
rows matching the predicate.
filter-rows
The filter-rows
function will return a data-frame
whose rows match
the predicate. The function signature is:
As an example, let’s filter mtcars
to find all the cars whose fuel
consumption is greater than 20 mpg:
To view them we’ll need to call the print-data
function directly instead
of using the print-object
function we installed earlier. Otherwise,
we’ll only see the first 6.
Filter predicates can be more complex than this, here’s an example
filtering the Vega movies
data set (which we call imdb
):
You can refer to any of the column/variable names in the data-frame
directly when constructing the filter predicate. The predicate is
turned into a lambda function, so let
, etc is also possible.
Summarising data
Often the first thing you’ll want to do with a data frame is get a quick summary. You can do that with these functions, and we’ve seen most of them used in this manual. For more information about these functions, see the data-frame api reference.
- nrow data-frame
- return the number of rows in data-frame
- ncol data-frame
- return the number of columns in data-frame
- dims data-frame
- return the dimensions of data-frame as a list in (rows columns) format
- keys data-frame
- return a vector of symbols representing column names
- column-names data-frame
- returns a list of strings of the column names in data-frames
- head data-frame &optional n
- displays the first n rows of data-frame. n defaults to 6.
- tail data-frame &optional n
- displays the last n rows of data-frame. n defaults to 6.
describe
- describe data-frame
- returns the meta-data for the variables in data-frame
describe
is a common lisp function that describes an object. In
Lisp-Stat describe
prints a description of the data frame and the
three ‘standard’ properties of the variables: type, unit and
description. It is similar to the str
command in R. To see an
example use the augmented mtcars
data set included in Lisp-Stat. In
this data set, we have added properties describing the variables.
This is a good illustration of why you should always save data frames
in lisp format; properties such as these are lost in CSV format.
summary
- summary data-frame
- returns a summary of the variables in data-frame
Summary functions are one of those things that tend to be use-case or application specific. Witness the number of R summary packages; there are at least half a dozen, including hmisc, stat.desc, psych describe, skim and summary tools. In short, there is no one-size-fits-all way to provide summaries, so Lisp-Stat provides the data structures upon which users can customise the summary output. The output you see below is a simple :print-function
for each of the summary structure types (numeric, factor, bit and generic).
Note that the model
column, essentially row-name
was deleted from
the output. The summary
function, designed for human readable
output, removes variables with all unique values, and those with
monotonically increasing numbers (usually row numbers).
To build your own summary function, use the get-summaries
function
to get a list of summary structures for the variables in the data
frame, and then print them as you wish.
columns
You can also describe or summarize individual columns:
Missing values
Data sets often contain missing values and we need to both understand
where and how many are missing, and how to transform or remove them
for downstream operations. In Lisp-Stat, missing values are
represented by the keyword symbol :na
. You can control this
encoding during delimited text import by passing an a-list
containing the mapping. By default this is a keyword parameter
map-alist
:
The default maps blank cells ("") and ones containing “NA” (not
available) to the keyword :na
, which stands for missing.
Some systems encode missing values as numeric, e.g. 99
; in this case
you can pass in a map-alist
that includes this mapping:
We will use the R air-quality dataset to illustrate working with missing values. Let’s load it now:
Examine
To see missing values we use the predicate missingp
. This works on
sequences, arrays and data-frames. It returns a logical sequence,
array or data-frame indicating which values are missing. T
indicates a missing value, NIL
means the value is present. Here’s
an example of using missingp
on a vector:
and on a data-frame:
We can see that the ozone
variable contains some missing values. To
see which rows of ozone
are missing, we can use the which
function:
and to get a count, use the length
function on this vector:
It’s often convenient to use the summary
function to get an overview
of missing values. We can do this because the missingp
function is
a transformation of a data-frame that yields another data-frame of
boolean values:
we can see that ozone
is missing 37 values, 24% of the total, and
solar-r
is missing 7 values.
Exclude
To exclude missing values from a single column, use the Common Lisp
remove
function:
To ensure that our data-frame includes only complete observations, we
exclude any row with a missing value. To do this use the
drop-missing
function:
Replace
To replace missing values we can use the transformation functions.
For example we can recode the missing values in ozone
by the mean.
Let’s look at the first six rows of the air quality data-frame:
Now replace ozone
with the mean using the common lisp function
nsubstitute
:
and look at head
again:
You could have used the non-destructive substitute
if you wanted to
create a new data-frame and leave the original aq
untouched.
Normally we’d round mean
to be consistent from a type perspective,
but did not here so you can see the values that were replaced.
Sampling
You can take a random sample of the rows of a data-frame with the select:sample
function:
You can also take random samples from CL sequences and arrays, with or without replacement and in various proportions. For further information see sampling in the select system manual.
Uses Vitter’s Algorithm
D to
efficiently select the rows. Sometimes you may want to use the
algorithm at a lower level. If you don’t want the sample itself, say you
only want the indices, you can directly use map-random-below
, which
simply calls a provided function on each index.
This is an enhancement and port to standard common lisp of ruricolist’s random-sample. It also removes the dependency on Trivia, which has a restrictive license (LLGPL).
Dates & Times
Lisp-Stat uses localtime to
represent dates. This works well, but the system is a bit strict on
input formats, and real-world data can be quite messy at times. For
these cases chronicity
and
cl-date-time-parser
can be helpful. Chronicity returns local-time
timestamp
objects,
and is particularly easy to work with.
For example, if you have a variable with dates encoded like: ‘Jan 7
1995’, you can recode the column like we did for the vega movies
data set:
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.