def extract_rank_stats(stats):
    """
    Extract the rank and number of plays of past days.

    **Parameters**

    - `stats`:   list of rankStats for each track within charts,
                    each element being a dictionary containing the
                    ranks (and plays if available) of previous days

    **Returns**

    A dictionary of previous day ranks (and plays if available), `{'ranks_{day}s_ago': ranks, 'plays_{day}s_ago': plays}`.
    """
    last = stats[-1]
    rank_stats = {"rank_today": last["rank"]}
    if "plays" in last.keys():
        rank_stats["plays_today"] = last["plays"]

    for i, stats_per_day in enumerate(stats[-2::-1]):
        rank_stats[f"rank_{i+1}d_ago"] = stats_per_day["rank"]
        if "plays" in stats_per_day.keys():
            rank_stats[f"plays_{i+1}d_ago"] = stats_per_day["plays"]
    return rank_stats