从 R 中的 jsonlite 解析列表中提取元素

Extracting elements from a jsonlite parsed list in R

我已经通过 jsonlite 将 .json 文件中的数据导入到 R 中。不幸的是,数据显示为 'List of 1',即使它包含多个类别。

library(jsonlite)
wimbledon <- from JSON("wimbledon.json",flatten=TRUE)

在全局环境中打开 wimbledon 时得到以下信息:

wimbledon                      List of 1
 graph_data: 'data.frame': 1 obs. of 2 variables:
 ..$ term:chr "wimbledon"
 ..$data: List of 1
 .. ..$: 'data.frame': 165 obs. of 3 variables:
 .. .. ..$ matches : int [1:165] 0 0 0 0 0 0 0 0 ...
 .. .. ..$ year : int [1:165] 1851 1852 1853 1854 ...

我的问题是,是否有任何简单的方法可以像我在数据框中使用 wimbledon$matches 和 wimbledon$year 那样仅引用 $matches 和 $year?我熟悉使用 wimbledon[[n]] 来提取列表的某个元素。但是,这里的问题是我似乎已经将所有内容解析为 jsonlite 中的单个列表。抱歉,我无法添加可重现的示例。在将 'matches' 和 'year' 列提取为单独的数据框列时,如果您能给我任何帮助,我们将不胜感激。

当列表中有未命名的值时,例如$: 'data.frame': 165 obs. of 3 variables:,需要var[[n]]语法,所以

wimbledon$graph_data$data[[1]]$matches

wimbledon$graph_data$data[[1]]$year

将return比赛和年份。