【问题标题】:vega: filter nth of each groupvega:过滤每组的第n个
【发布时间】:2021-04-18 01:44:24
【问题描述】:

如果我要按日期分组,我将如何过滤每个组的第 nth 个条目?

{
  "$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"}
  }
}

编辑

让我们保持这个数据不可知,因为我的数据有很多列,我想要完整的行。

【问题讨论】:

    标签: unique vega-lite vega


    【解决方案1】:

    转换概述:

    1. 将时间转换为日期以进行分组。
    2. 按日期分组并对组内的每一行进行编号。
    3. 过滤 nth 行。
    {
      "$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"}
      }
    }
    

    缺点是vega编辑器在加入window transform后变得很慢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-16
      • 2023-03-29
      • 1970-01-01
      • 2017-04-17
      • 1970-01-01
      • 1970-01-01
      • 2016-12-12
      • 1970-01-01
      相关资源
      最近更新 更多