Book Image

Clojure Data Analysis Cookbook - Second Edition

By : Eric Richard Rochester
Book Image

Clojure Data Analysis Cookbook - Second Edition

By: Eric Richard Rochester

Overview of this book

Table of Contents (19 chapters)
Clojure Data Analysis Cookbook Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Filtering datasets with $where


While we can filter datasets before we import them into Incanter, Incanter makes it easy to filter and create new datasets from the existing ones. We'll take a look at its query language in this recipe.

Getting ready

We'll use the same dependencies, imports, and data as we did in the Selecting columns with $ recipe.

How to do it…

Once we have the data, we query it using the $where function:

  1. For example, this creates a dataset with a row for the percentage of China's total land area that is used for agriculture:

    user=> (def land-use
             (i/$where {:Indicator-Code "AG.LND.AGRI.ZS"}
                       chn-data))
    user=> (i/nrow land-use)
    1
    user=> (i/$ [:Indicator-Code :2000] land-use)
    ("AG.LND.AGRI.ZS" "56.2891584865366")
  2. The queries can be more complicated too. This expression picks out the data that exists for 1962 by filtering any empty strings in that column:

    user=> (i/$ (range 5) [:Indicator-Code :1962]
             (i/$where {:1962 {:ne ""}} chn-data...