Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在数据帧中仅获取json中的特定对象_Json_R - Fatal编程技术网

在数据帧中仅获取json中的特定对象

在数据帧中仅获取json中的特定对象,json,r,Json,R,我想将json文件中的单个对象导入到R数据帧中。通常我使用jsonlite包中的fromJSON()。但是现在我想加载到数据帧中,然后只有名为的对象播放 如果我使用: library(jsonlite) df <- fromJSON("http://live.nhl.com/GameData/20132014/2013020555/PlayByPlay.json") library(jsonlite) df返回一个数据帧,尽管它是一个列表和数据帧的组合。如果您使用不同的包,它只是一个列表

我想将json文件中的单个对象导入到R数据帧中。通常我使用jsonlite包中的
fromJSON()
。但是现在我想加载到数据帧中,然后只有名为的对象播放

如果我使用:

library(jsonlite)
df <- fromJSON("http://live.nhl.com/GameData/20132014/2013020555/PlayByPlay.json")
library(jsonlite)

df返回一个数据帧,尽管它是一个列表和数据帧的组合。如果您使用不同的包,它只是一个列表。使用str(df)(警告…长输出)

。。。。显示可以通过以下方式获得
播放
部分:

plays_out <- df$data$game$plays

我想我更喜欢RJSONIO::fromJSON的结果,因为它不会增加数据帧强制的复杂性。

如果我这样做了,我不确定谁错过了什么。我在“RJSONIO”包中使用了
fromJSON
函数,在我注意到您在文本中提到“jsonlite”(不带引号)之前,我加载了该包。这就是为什么我增加了图书馆电话。我想这可能是因为我们正在研究的结构不同。谢谢你的更新。我切换到“RJSONIO”软件包,这很管用。现在,我正努力将这个列表放到一个data.frame中。我已经试过了,从plyr软件包,
df哪个列表?如果您想使用第一个
aoi
值,它将是
as.data.frame(df$game$plays$play$data$aoi)
plays_out <- df$data$game$plays
> str(df )
'data.frame':   1 obs. of  2 variables:
 $ refreshInterval:List of 1
  ..$ data: num 0
 $ game           :'data.frame':    1 obs. of  7 variables:
  ..$ awayteamid  :List of 1
  .. ..$ data: num 24
  ..$ awayteamname:List of 1
  .. ..$ data: chr "Anaheim Ducks"
  ..$ hometeamname:List of 1
  .. ..$ data: chr "Washington Capitals"
  ..$ plays       :'data.frame':    1 obs. of  1 variable:
  .. ..$ play:List of 1
  .. .. ..$ data:'data.frame':  102 obs. of  29 variables:
  .. .. .. ..$ aoi          :List of 102
  .. .. .. .. ..$ : num  8470612 8470621 8473933 8473972 8475151 ...
  .. .. .. .. ..$ : num  8459442 8467332 8467400 8471476 8471699 ...
  .. .. .. .. ..$ : num  8459442 8467332 8467400 8471476 8471699 ...
  .. .. .. .. ..$ : num  8459442 8467332 8467400 8471476 8471699 ...
#------snipped output------------
> length(df$game$plays)
[1] 1
> length(df$game$plays$play)
[1] 1
> length(df$game$plays$play$data)
[1] 29