Reading data from Excel with Incanter
We've seen how Incanter makes a lot of common data-processing tasks very simple, and reading an Excel spreadsheet is another example of this.
Getting ready
First, make sure that your Leiningen project.clj
file contains the right dependencies:
(defproject getting-data "0.1.0-SNAPSHOT" :dependencies [[org.clojure/clojure "1.6.0"] [incanter "1.5.5"]"]])
Also, make sure that you've loaded those packages into the REPL or script:
(use 'incanter.core 'incanter.excel)
Find the Excel spreadsheet you want to work on. The file name of my spreadsheet is data/small-sample-header.xls
, as shown in the following screenshot. You can download this from http://www.ericrochester.com/clj-data-analysis/data/small-sample-header.xls.
How to do it…
Now, all you need to do is call incanter.excel/read-xls
:
user=> (read-xls "data/small-sample-header.xls") | given-name | surname | relation | |------------+---------+-------------| | Gomez | Addams | father | | Morticia | Addams | mother | | Pugsley | Addams | brother |
How it works…
This can read standard Excel files (.xls) and the XML-based file format introduced in Excel 2003 (.xlsx).