Search code examples
uniquevega-litevega

vega: filter nth of each group


If I were to group by date, how would I filter the nth entry of each group?

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "data": {"url": "https://cdn.jsdelivr.net/npm/vega-datasets@v1.29.0/data/seattle-temps.csv"},
  "mark": "point",
  "encoding": {
    "x": {"field": "date", "type": "temporal"},
    "y": {"field": "temp", "type": "quantitative"}
  }
}

edit

Let's keep this data-agnostic as my data has many columns and I would like rows in their entirety.


Solution

  • Transforms overview:

    1. Convert times to dates for grouping.
    2. Group by date and number each row within the groups.
    3. Filter on nth row.
    {
      "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
      "data": {"url": "https://cdn.jsdelivr.net/npm/vega-datasets@v1.29.0/data/seattle-temps.csv"},
      "transform": [
        {"timeUnit": "yearmonthdate", "field": "date", "as": "date"},
        {
          "window": [{"op": "row_number", "as": "row"}],
          "groupby": ["date"]
        },
        {"calculate": "datum.index", "as":"newnew"},
        {"filter": "datum['row'] == 1"}
      ],
      "mark": "point",
      "encoding": {
        "x": {"field": "date", "type": "temporal"},
        "y": {"field": "temp", "type": "quantitative"}
      }
    }
    

    The downside is that the vega editor becomes very slow after adding the window transform.