You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在R中展平MTG JSON数据集并转换为按releaseDate排序的数据框

Flattening MTGJSON's AllSets.json and Sorting by Release Date

Got it, let's work through flattening that tricky MTGJSON nested structure and getting your sorted data frame sorted out! I’ve messed around with this dataset before, so I know those nested lists can feel like a maze. Here’s a step-by-step solution that should work:

1. Install & Load Required Packages

First, make sure you have the necessary tools installed. If you haven’t already, run this to grab the packages we’ll need:

install.packages(c("jsonlite", "tidyr", "dplyr"))

Then load them into your R session:

library(jsonlite)
library(tidyr)
library(dplyr)

2. Read the Compressed JSON File

Good news: you don’t need to manually unzip the file first—jsonlite::fromJSON can read directly from the zip archive. We’ll use the flatten parameter to handle the first layer of nested structures right away:

# Read the zipped JSON and flatten top-level nested fields
mtg_raw <- fromJSON("AllSets.json.zip", flatten = TRUE)

# The top level is a named list (each entry is a Magic set), so convert it to a data frame
# We add a `set_code` column to keep track of which set each entry comes from
mtg_sets <- bind_rows(mtg_raw, .id = "set_code")

3. Unnest the Nested Card Data

Each set entry has a cards field that’s a nested list of all the cards in that set. We’ll use unnest to explode this into individual rows—one row per card, with all the set-level info attached:

# Flatten the cards column into individual rows
mtg_flattened <- mtg_sets %>%
  unnest(cols = c(cards), keep_empty = TRUE)  # keep_empty keeps sets with no cards (if any exist)

4. Sort by Release Date

Finally, we’ll sort the data frame by releaseDate. Important: we’ll convert this field to a proper date format first—otherwise, R will sort it as a string, which can lead to weird ordering (like "2023-10" coming before "2023-01"):

# Convert releaseDate to date format and sort
mtg_sorted <- mtg_flattened %>%
  mutate(releaseDate = as.Date(releaseDate)) %>%
  arrange(releaseDate)

Bonus: Handling Deeply Nested Fields

If you still have leftover nested fields (like card legalities or rulings), you can keep unnesting them as needed. For example, to flatten the legalities field:

mtg_sorted <- mtg_sorted %>%
  unnest(cols = c(legalities), keep_empty = TRUE)

Verify Your Result

Check the output to make sure everything looks right:

# Preview the first 5 rows
head(mtg_sorted)

# Inspect the structure to confirm no more unwanted nesting
str(mtg_sorted)

内容的提问来源于stack exchange,提问作者Davide Lorino

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:36:21