Selecting rows with $

The Incanter macro $ also pulls rows out of a dataset. In this recipe, we'll see this in action.

Getting ready

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

How to do it…

Similar to how we use $ in order to select columns, there are several ways in which we can use it to select rows, shown as follows:

  1. We can create a sequence of the values of one row using $, and pass it the index of the row we want as well as passing :all for the columns:
    user=> (i/$ 0 :all chn-data)
    ("AG.AGR.TRAC.NO" "684290" "738526" "52661" "" "880859" "" "" "" "59657" "847916" "862078" "891170" "235524" "126440" "469106" "282282" "817857" "125442" "703117" "CHN" "66290" "705723" "824113" "" "151281" "669675" "861364" "559638" "191220" "180772" "73021" "858031" "734325" "Agricultural machinery, tractors" "100432" "" "796867" "" "China" "" "" "155602" "" "" "770629" "747900" "346786" "" "398946" "876470" "" "795713" "" "55360" "685202" "989139" "798506" "")
  2. We can also pull out a dataset containing multiple rows by passing more than one index into $ with a vector (There's a lot of data, even for three rows, so I won't show it here):
    (i/$ (range 3) :all chn-data)
  3. We can also combine the two ways to slice data in order to pull specific columns and rows. We can either pull out a single row or multiple rows:
    user=> (i/$ 0 [:Indicator-Code :1992] chn-data)
    ("AG.AGR.TRAC.NO" "770629")
    user=> (i/$ (range 3) [:Indicator-Code :1992] chn-data)
    
    |   :Indicator-Code |  :1992 |
    |-------------------+--------|
    |    AG.AGR.TRAC.NO | 770629 |
    | AG.CON.FERT.PT.ZS |        |
    |    AG.CON.FERT.ZS |        |

How it works…

The $ macro is the workhorse used to slice rows and project (or select) columns from datasets. When it's called with two indexing parameters, the first is the row or rows and the second is the column or columns.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.56.12