I have a list that I extracted from an API in JSON format. I was able to extract the data using jsonlite
. The level of JSON
data that I am trying to explore is a df
that is made up of one of the columns titled player
that has additional columns that are giving me issues:
- Wildcards:
df
with 2 columns - Opponents: List
- Icons:
df
with 4 columns
I've tried to unnest
the players column and get the following error:
Error in bind_rows_(x, .id) :
Argument 2 can't be a list containing data frames
I subset my data as follows:
json <-
url %>%
fromJSON()
json_scrape <- json$body$rosters
JSON_list <- json_scrape$teams[1, ]
# Reproduced list from JSON Scrape. Represents one element (team)\
JSON_list <- structure(list(
short_name = "Kramerica", total_roster_salary = 22L,
players = list(structure(list(
wildcards = structure(list(
contract = c("1", "1"),
salary = c("1", "21")), class = "data.frame", row.names = c(NA, 2L)),
photo = c(
"http://sports.cbsimg.net/images/baseball/mlb/players/170x170/1657581.png",
"http://sports.cbsimg.net/images/baseball/mlb/players/170x170/1670417.png"),
opponents = list(
structure(list(
abbrev = c("OAK", "OAK"),time = c(1553803620L,1553911620L),
date = c("20190328","20190329")), class = "data.frame", row.names = c(NA, 2L)),
structure(list(
abbrev = c("TEX", "TEX", "TEX"), time = c(1553803500L, 1553990700L, 1554062700L),
date = c("20190328", "20190330", "20190331")), class = "data.frame", row.names = c(NA,3L))),
icons = structure(list(
hot = c(NA, 1L),
cold = c(1L, NA),
injury = c("Knee: Questionable for start of season",NA)), class = "data.frame", row.names = c(NA, 21L)),
percentstarted = c("48%", "97%"),
profile_link = c(
"<a class='playerLink' aria-label=' Jonathan Lucroy C LAA' href='http://baseball.cbssports.com/players/playerpage/1657581'>Jonathan Lucroy</a> <span class=\"playerPositionAndTeam\">C | LAA</span> ",
"<a class='playerLink' aria-label=' Anthony Rizzo 1B CHC' href='http://baseball.cbssports.com/players/playerpage/1670417'>Anthony Rizzo</a> <span class=\"playerPositionAndTeam\">1B | CHC</span>"),
id = c("1657581", "1670417"),
jersey = c("20", "44"),
percentowned = c("61%", "99%"),
pro_team = c("LAA", "CHC"),
eligible = c("C,U", "1B,U"),
owned_by_team_id = c(12L, 12L),
profile_url = c(
"http://baseball.cbssports.com/players/playerpage/1657581",
"http://baseball.cbssports.com/players/playerpage/1670417"),
fullname = c("Jonathan Lucroy", "Anthony Rizzo"),
injury = c(NA, "Knee"),
return = c("Questionable for start of season", NA)), class = "data.frame", row.names = c(NA, 2L))),
name = "Kramerica Enterprises", logo = "http://baseball.cbssports.com/images/team-logo/main-36x36.jpg",
abbr = "KE", id = "12", active_roster_salary = 22L,
warning = structure(list(description = NA_character_), row.names = 1L, class = "data.frame")
), row.names = 1L, class = "data.frame")
The df
I'm looking for would have each team with it's own row. Here's a sample:
# Sample does not include all data frames. Each row would be a player
tibble::tribble(
~short_name, ~total_roster_salary, ~contract, ~salary, ~abbrev, ~hot, ~id2,
"Kramerica", 22, 1, 1, "OAK", NA, 1657581,
"Kramerica", 2, 1, 21, "OAK", NA, 1657581
)
I'm looking to create a comprehensive df
that binds all of the rows for every team from the initial df
. I'm guessing that some purrr
function could be used, but I'm not sure how to use it.