我在将 JSON 文件(从 API)转换为 R 中的数据帧时遇到问题。例如 URL
我尝试了 S/O 的一些不同建议,包括将json数据转换为R中的数据框 https://stackoverflow.com/questions/28683769/convert-json-data-to-data-frame-in-r以及各种博客文章,例如http://zevross.com/blog/2015/02/12/using-r-to-download-and-parse-json-an-example-using-data-from-an-open-data-portal/ http://zevross.com/blog/2015/02/12/using-r-to-download-and-parse-json-an-example-using-data-from-an-open-data-portal/
我最接近的是使用下面的代码,它为我提供了一个包含 4 个“行”和一堆“变量”(V1、V2 等)的大矩阵。我假设这个 JSON 文件的格式与“正常”文件的格式不同。
library(RJSONIO)
raw_data <- getURL("http://api.fantasy.nfl.com/v1/players/stats?statType=seasonStats&season=2010&week=1&format=json")
data <- fromJSON(raw_data)
final_data <- do.call(rbind, data)
我对如何让它工作非常不可知,所以任何 R 包/过程都是受欢迎的。提前致谢。
The jsonlite
包自动获取数据框:
library(jsonlite)
mydata <- fromJSON("http://api.fantasy.nfl.com/v1/players/stats?statType=seasonStats&season=2010&week=1&format=json")
names(mydata$players)
# [1] "id" "esbid" "gsisPlayerId" "name"
# [5] "position" "teamAbbr" "stats" "seasonPts"
# [9] "seasonProjectedPts" "weekPts" "weekProjectedPts"
head(mydata$players)
# id esbid gsisPlayerId name position teamAbbr stats.1
# 1 100029 FALSE FALSE San Francisco 49ers DEF SF 16
# 2 729 ABD660476 00-0025940 Husain Abdullah DB KC 15
# 3 2504171 ABR073003 00-0019546 John Abraham LB 15
# 4 2507266 ADA509576 00-0025668 Michael Adams DB 13
# 5 2505708 ADA515576 00-0022247 Mike Adams DB IND 15
# 6 1037889 ADA534252 00-0027610 Phillip Adams DB ATL 11
您可以使用以下命令来控制它simplify
中的论点jsonlite::fromJSON()
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)