使用purrr :: map在R中取消嵌套JSON时会丢失名称(取消列表,t,as_tibble)

时间:2019-03-22 04:49:55

标签: r json dataframe dplyr purrr

为大量初始数据共享而道歉,但会有所帮助,因为它将使问题可重现。根据R中的比赛数据,我有一个很大的篮球比赛JSON,我想将其展平为一个数据框。这是JSON中的前6个项目,作为R中的列表:

my.pbp.data = list(structure(list(id = "0e2d8178-dfb7-4159-953e-f1851a27aee8", 
    clock = "20:00", updated = "2019-03-07T01:05:42+00:00", description = "Cyclones lineup change (Nick Weiler-Babb, Lindell Wigginton, Talen Horton-Tucker, Michael Jacobson, Tyrese Haliburton)", 
    event_type = "lineupchange", attribution = structure(list(
        name = "Cyclones", market = "Iowa State", id = "0e4258fa-3154-4c16-b693-adecab184c6c", 
        team_basket = "right"), .Names = c("name", "market", 
    "id", "team_basket")), location = structure(list(coord_x = 0L, 
        coord_y = 0L), .Names = c("coord_x", "coord_y")), on_court = structure(list(
        home = structure(list(name = "Mountaineers", market = "West Virginia", 
            id = "f770d3a2-221c-49ce-9c7a-680f7417fe9e", players = list(
                structure(list(full_name = "Derek Culver", jersey_number = "1", 
                  id = "9b4cdef4-5b76-4981-ad7a-14abbb425677"), .Names = c("full_name", 
                "jersey_number", "id")), structure(list(full_name = "Jordan McCabe", 
                  jersey_number = "5", id = "a1bfd18b-7d51-4164-942c-8dc73df868bb"), .Names = c("full_name", 
                "jersey_number", "id")), structure(list(full_name = "Jermaine Haley", 
                  jersey_number = "10", id = "453d63d6-74ce-4169-9405-e0b2444e45f2"), .Names = c("full_name", 
                "jersey_number", "id")), structure(list(full_name = "Chase Harler", 
                  jersey_number = "14", id = "8a7e4346-4933-40ab-90f5-017fce37efed"), .Names = c("full_name", 
                "jersey_number", "id")), structure(list(full_name = "Lamont West", 
                  jersey_number = "15", id = "b191d2a3-19d5-48ed-b4c4-a55b6e039d8d"), .Names = c("full_name", 
                "jersey_number", "id")))), .Names = c("name", 
        "market", "id", "players")), away = structure(list(name = "Cyclones", 
            market = "Iowa State", id = "0e4258fa-3154-4c16-b693-adecab184c6c", 
            players = list(structure(list(full_name = "Nick Weiler-Babb", 
                jersey_number = "1", id = "067af76a-e3d7-439e-8a8a-b662f3ec08de"), .Names = c("full_name", 
            "jersey_number", "id")), structure(list(full_name = "Lindell Wigginton", 
                jersey_number = "5", id = "5c09bc24-a9e0-427f-b611-70231a651bdf"), .Names = c("full_name", 
            "jersey_number", "id")), structure(list(full_name = "Talen Horton-Tucker", 
                jersey_number = "11", id = "7513239c-27a3-455c-a670-167b3f23a071"), .Names = c("full_name", 
            "jersey_number", "id")), structure(list(full_name = "Michael Jacobson", 
                jersey_number = "12", id = "0ad642b3-b38e-47f7-adac-34039306826f"), .Names = c("full_name", 
            "jersey_number", "id")), structure(list(full_name = "Tyrese Haliburton", 
                jersey_number = "22", id = "82cee878-39a7-4fd2-a38f-76be4dd4cea4"), .Names = c("full_name", 
            "jersey_number", "id")))), .Names = c("name", "market", 
        "id", "players"))), .Names = c("home", "away"))), .Names = c("id", 
"clock", "updated", "description", "event_type", "attribution", 
"location", "on_court")), structure(list(id = "b7b050c2-21f1-4860-af7b-ec443301ba13", 
    clock = "20:00", updated = "2019-03-07T01:02:20+00:00", description = "Mountaineers lineup change (Derek Culver, Jordan McCabe, Jermaine Haley, Emmitt Matthews Jr., Lamont West)", 
    event_type = "lineupchange", attribution = structure(list(
        name = "Mountaineers", market = "West Virginia", id = "f770d3a2-221c-49ce-9c7a-680f7417fe9e", 
        team_basket = "left"), .Names = c("name", "market", "id", 
    "team_basket")), location = structure(list(coord_x = 0L, 
        coord_y = 0L), .Names = c("coord_x", "coord_y")), on_court = structure(list(
        home = structure(list(name = "Mountaineers", market = "West Virginia", 
            id = "f770d3a2-221c-49ce-9c7a-680f7417fe9e", players = list(
                structure(list(full_name = "Derek Culver", jersey_number = "1", 
                  id = "9b4cdef4-5b76-4981-ad7a-14abbb425677"), .Names = c("full_name", 
                "jersey_number", "id")), structure(list(full_name = "Jordan McCabe", 
                  jersey_number = "5", id = "a1bfd18b-7d51-4164-942c-8dc73df868bb"), .Names = c("full_name", 
                "jersey_number", "id")), structure(list(full_name = "Jermaine Haley", 
                  jersey_number = "10", id = "453d63d6-74ce-4169-9405-e0b2444e45f2"), .Names = c("full_name", 
                "jersey_number", "id")), structure(list(full_name = "Emmitt Matthews Jr.", 
                  jersey_number = "11", id = "70f012ae-5deb-42da-9fff-94ac68ae36a7"), .Names = c("full_name", 
                "jersey_number", "id")), structure(list(full_name = "Lamont West", 
                  jersey_number = "15", id = "b191d2a3-19d5-48ed-b4c4-a55b6e039d8d"), .Names = c("full_name", 
                "jersey_number", "id")))), .Names = c("name", 
        "market", "id", "players")), away = structure(list(name = "Cyclones", 
            market = "Iowa State", id = "0e4258fa-3154-4c16-b693-adecab184c6c", 
            players = list(structure(list(full_name = "Nick Weiler-Babb", 
                jersey_number = "1", id = "067af76a-e3d7-439e-8a8a-b662f3ec08de"), .Names = c("full_name", 
            "jersey_number", "id")), structure(list(full_name = "Lindell Wigginton", 
                jersey_number = "5", id = "5c09bc24-a9e0-427f-b611-70231a651bdf"), .Names = c("full_name", 
            "jersey_number", "id")), structure(list(full_name = "Talen Horton-Tucker", 
                jersey_number = "11", id = "7513239c-27a3-455c-a670-167b3f23a071"), .Names = c("full_name", 
            "jersey_number", "id")), structure(list(full_name = "Michael Jacobson", 
                jersey_number = "12", id = "0ad642b3-b38e-47f7-adac-34039306826f"), .Names = c("full_name", 
            "jersey_number", "id")), structure(list(full_name = "Tyrese Haliburton", 
                jersey_number = "22", id = "82cee878-39a7-4fd2-a38f-76be4dd4cea4"), .Names = c("full_name", 
            "jersey_number", "id")))), .Names = c("name", "market", 
        "id", "players"))), .Names = c("home", "away"))), .Names = c("id", 
"clock", "updated", "description", "event_type", "attribution", 
"location", "on_court")), structure(list(id = "4b748b1e-2f0f-4dbf-9b05-452dce611c7c", 
    clock = "20:00", updated = "2019-03-07T01:02:01+00:00", description = "Open inbound Cyclones", 
    event_type = "openinbound", attribution = structure(list(
        name = "Cyclones", market = "Iowa State", id = "0e4258fa-3154-4c16-b693-adecab184c6c", 
        team_basket = "right"), .Names = c("name", "market", 
    "id", "team_basket")), location = structure(list(coord_x = 455L, 
        coord_y = 384L), .Names = c("coord_x", "coord_y")), possession = structure(list(
        name = "Cyclones", market = "Iowa State", id = "0e4258fa-3154-4c16-b693-adecab184c6c"), .Names = c("name", 
    "market", "id")), on_court = structure(list(home = structure(list(
        name = "Mountaineers", market = "West Virginia", id = "f770d3a2-221c-49ce-9c7a-680f7417fe9e", 
        players = list(structure(list(full_name = "Derek Culver", 
            jersey_number = "1", id = "9b4cdef4-5b76-4981-ad7a-14abbb425677"), .Names = c("full_name", 
        "jersey_number", "id")), structure(list(full_name = "Jordan McCabe", 
            jersey_number = "5", id = "a1bfd18b-7d51-4164-942c-8dc73df868bb"), .Names = c("full_name", 
        "jersey_number", "id")), structure(list(full_name = "Jermaine Haley", 
            jersey_number = "10", id = "453d63d6-74ce-4169-9405-e0b2444e45f2"), .Names = c("full_name", 
        "jersey_number", "id")), structure(list(full_name = "Emmitt Matthews Jr.", 
            jersey_number = "11", id = "70f012ae-5deb-42da-9fff-94ac68ae36a7"), .Names = c("full_name", 
        "jersey_number", "id")), structure(list(full_name = "Lamont West", 
            jersey_number = "15", id = "b191d2a3-19d5-48ed-b4c4-a55b6e039d8d"), .Names = c("full_name", 
        "jersey_number", "id")))), .Names = c("name", "market", 
    "id", "players")), away = structure(list(name = "Cyclones", 
        market = "Iowa State", id = "0e4258fa-3154-4c16-b693-adecab184c6c", 
        players = list(structure(list(full_name = "Nick Weiler-Babb", 
            jersey_number = "1", id = "067af76a-e3d7-439e-8a8a-b662f3ec08de"), .Names = c("full_name", 
        "jersey_number", "id")), structure(list(full_name = "Lindell Wigginton", 
            jersey_number = "5", id = "5c09bc24-a9e0-427f-b611-70231a651bdf"), .Names = c("full_name", 
        "jersey_number", "id")), structure(list(full_name = "Talen Horton-Tucker", 
            jersey_number = "11", id = "7513239c-27a3-455c-a670-167b3f23a071"), .Names = c("full_name", 
        "jersey_number", "id")), structure(list(full_name = "Michael Jacobson", 
            jersey_number = "12", id = "0ad642b3-b38e-47f7-adac-34039306826f"), .Names = c("full_name", 
        "jersey_number", "id")), structure(list(full_name = "Tyrese Haliburton", 
            jersey_number = "22", id = "82cee878-39a7-4fd2-a38f-76be4dd4cea4"), .Names = c("full_name", 
        "jersey_number", "id")))), .Names = c("name", "market", 
    "id", "players"))), .Names = c("home", "away"))), .Names = c("id", 
"clock", "updated", "description", "event_type", "attribution", 
"location", "possession", "on_court")), structure(list(id = "14d746d7-f213-43be-8023-df69ea1aba60", 
    clock = "19:40", updated = "2019-03-07T01:02:26+00:00", description = "Jermaine Haley misses two point jump shot", 
    event_type = "twopointmiss", attribution = structure(list(
        name = "Mountaineers", market = "West Virginia", id = "f770d3a2-221c-49ce-9c7a-680f7417fe9e", 
        team_basket = "left"), .Names = c("name", "market", "id", 
    "team_basket")), location = structure(list(coord_x = 160L, 
        coord_y = 390L), .Names = c("coord_x", "coord_y")), on_court = structure(list(
        home = structure(list(name = "Mountaineers", market = "West Virginia", 
            id = "f770d3a2-221c-49ce-9c7a-680f7417fe9e", players = list(
                structure(list(full_name = "Derek Culver", jersey_number = "1", 
                  id = "9b4cdef4-5b76-4981-ad7a-14abbb425677"), .Names = c("full_name", 
                "jersey_number", "id")), structure(list(full_name = "Jordan McCabe", 
                  jersey_number = "5", id = "a1bfd18b-7d51-4164-942c-8dc73df868bb"), .Names = c("full_name", 
                "jersey_number", "id")), structure(list(full_name = "Jermaine Haley", 
                  jersey_number = "10", id = "453d63d6-74ce-4169-9405-e0b2444e45f2"), .Names = c("full_name", 
                "jersey_number", "id")), structure(list(full_name = "Emmitt Matthews Jr.", 
                  jersey_number = "11", id = "70f012ae-5deb-42da-9fff-94ac68ae36a7"), .Names = c("full_name", 
                "jersey_number", "id")), structure(list(full_name = "Lamont West", 
                  jersey_number = "15", id = "b191d2a3-19d5-48ed-b4c4-a55b6e039d8d"), .Names = c("full_name", 
                "jersey_number", "id")))), .Names = c("name", 
        "market", "id", "players")), away = structure(list(name = "Cyclones", 
            market = "Iowa State", id = "0e4258fa-3154-4c16-b693-adecab184c6c", 
            players = list(structure(list(full_name = "Nick Weiler-Babb", 
                jersey_number = "1", id = "067af76a-e3d7-439e-8a8a-b662f3ec08de"), .Names = c("full_name", 
            "jersey_number", "id")), structure(list(full_name = "Lindell Wigginton", 
                jersey_number = "5", id = "5c09bc24-a9e0-427f-b611-70231a651bdf"), .Names = c("full_name", 
            "jersey_number", "id")), structure(list(full_name = "Talen Horton-Tucker", 
                jersey_number = "11", id = "7513239c-27a3-455c-a670-167b3f23a071"), .Names = c("full_name", 
            "jersey_number", "id")), structure(list(full_name = "Michael Jacobson", 
                jersey_number = "12", id = "0ad642b3-b38e-47f7-adac-34039306826f"), .Names = c("full_name", 
            "jersey_number", "id")), structure(list(full_name = "Tyrese Haliburton", 
                jersey_number = "22", id = "82cee878-39a7-4fd2-a38f-76be4dd4cea4"), .Names = c("full_name", 
            "jersey_number", "id")))), .Names = c("name", "market", 
        "id", "players"))), .Names = c("home", "away")), statistics = list(
        structure(list(type = "fieldgoal", made = FALSE, shot_type = "jump shot", 
            team = structure(list(name = "Mountaineers", market = "West Virginia", 
                id = "f770d3a2-221c-49ce-9c7a-680f7417fe9e"), .Names = c("name", 
            "market", "id")), player = structure(list(full_name = "Jermaine Haley", 
                jersey_number = "10", id = "453d63d6-74ce-4169-9405-e0b2444e45f2"), .Names = c("full_name", 
            "jersey_number", "id"))), .Names = c("type", "made", 
        "shot_type", "team", "player")))), .Names = c("id", "clock", 
"updated", "description", "event_type", "attribution", "location", 
"on_court", "statistics")), structure(list(id = "68ad7e77-8254-4978-bad0-69d5c5c854e8", 
    clock = "19:38", updated = "2019-03-07T01:02:26+00:00", description = "Talen Horton-Tucker defensive rebound", 
    event_type = "rebound", attribution = structure(list(name = "Cyclones", 
        market = "Iowa State", id = "0e4258fa-3154-4c16-b693-adecab184c6c", 
        team_basket = "right"), .Names = c("name", "market", 
    "id", "team_basket")), location = structure(list(coord_x = 160L, 
        coord_y = 390L), .Names = c("coord_x", "coord_y")), possession = structure(list(
        name = "Cyclones", market = "Iowa State", id = "0e4258fa-3154-4c16-b693-adecab184c6c"), .Names = c("name", 
    "market", "id")), on_court = structure(list(home = structure(list(
        name = "Mountaineers", market = "West Virginia", id = "f770d3a2-221c-49ce-9c7a-680f7417fe9e", 
        players = list(structure(list(full_name = "Derek Culver", 
            jersey_number = "1", id = "9b4cdef4-5b76-4981-ad7a-14abbb425677"), .Names = c("full_name", 
        "jersey_number", "id")), structure(list(full_name = "Jordan McCabe", 
            jersey_number = "5", id = "a1bfd18b-7d51-4164-942c-8dc73df868bb"), .Names = c("full_name", 
        "jersey_number", "id")), structure(list(full_name = "Jermaine Haley", 
            jersey_number = "10", id = "453d63d6-74ce-4169-9405-e0b2444e45f2"), .Names = c("full_name", 
        "jersey_number", "id")), structure(list(full_name = "Emmitt Matthews Jr.", 
            jersey_number = "11", id = "70f012ae-5deb-42da-9fff-94ac68ae36a7"), .Names = c("full_name", 
        "jersey_number", "id")), structure(list(full_name = "Lamont West", 
            jersey_number = "15", id = "b191d2a3-19d5-48ed-b4c4-a55b6e039d8d"), .Names = c("full_name", 
        "jersey_number", "id")))), .Names = c("name", "market", 
    "id", "players")), away = structure(list(name = "Cyclones", 
        market = "Iowa State", id = "0e4258fa-3154-4c16-b693-adecab184c6c", 
        players = list(structure(list(full_name = "Nick Weiler-Babb", 
            jersey_number = "1", id = "067af76a-e3d7-439e-8a8a-b662f3ec08de"), .Names = c("full_name", 
        "jersey_number", "id")), structure(list(full_name = "Lindell Wigginton", 
            jersey_number = "5", id = "5c09bc24-a9e0-427f-b611-70231a651bdf"), .Names = c("full_name", 
        "jersey_number", "id")), structure(list(full_name = "Talen Horton-Tucker", 
            jersey_number = "11", id = "7513239c-27a3-455c-a670-167b3f23a071"), .Names = c("full_name", 
        "jersey_number", "id")), structure(list(full_name = "Michael Jacobson", 
            jersey_number = "12", id = "0ad642b3-b38e-47f7-adac-34039306826f"), .Names = c("full_name", 
        "jersey_number", "id")), structure(list(full_name = "Tyrese Haliburton", 
            jersey_number = "22", id = "82cee878-39a7-4fd2-a38f-76be4dd4cea4"), .Names = c("full_name", 
        "jersey_number", "id")))), .Names = c("name", "market", 
    "id", "players"))), .Names = c("home", "away")), statistics = list(
        structure(list(type = "rebound", rebound_type = "defensive", 
            team = structure(list(name = "Cyclones", market = "Iowa State", 
                id = "0e4258fa-3154-4c16-b693-adecab184c6c"), .Names = c("name", 
            "market", "id")), player = structure(list(full_name = "Talen Horton-Tucker", 
                jersey_number = "11", id = "7513239c-27a3-455c-a670-167b3f23a071"), .Names = c("full_name", 
            "jersey_number", "id"))), .Names = c("type", "rebound_type", 
        "team", "player")))), .Names = c("id", "clock", "updated", 
"description", "event_type", "attribution", "location", "possession", 
"on_court", "statistics")), structure(list(id = "d6581039-4c99-49c4-8a9d-f00d70fa484a", 
    clock = "19:28", updated = "2019-03-07T01:02:37+00:00", description = "Talen Horton-Tucker makes three point jump shot (Tyrese Haliburton assists)", 
    event_type = "threepointmade", attribution = structure(list(
        name = "Cyclones", market = "Iowa State", id = "0e4258fa-3154-4c16-b693-adecab184c6c", 
        team_basket = "right"), .Names = c("name", "market", 
    "id", "team_basket")), location = structure(list(coord_x = 799L, 
        coord_y = 163L), .Names = c("coord_x", "coord_y")), possession = structure(list(
        name = "Mountaineers", market = "West Virginia", id = "f770d3a2-221c-49ce-9c7a-680f7417fe9e"), .Names = c("name", 
    "market", "id")), on_court = structure(list(home = structure(list(
        name = "Mountaineers", market = "West Virginia", id = "f770d3a2-221c-49ce-9c7a-680f7417fe9e", 
        players = list(structure(list(full_name = "Derek Culver", 
            jersey_number = "1", id = "9b4cdef4-5b76-4981-ad7a-14abbb425677"), .Names = c("full_name", 
        "jersey_number", "id")), structure(list(full_name = "Jordan McCabe", 
            jersey_number = "5", id = "a1bfd18b-7d51-4164-942c-8dc73df868bb"), .Names = c("full_name", 
        "jersey_number", "id")), structure(list(full_name = "Jermaine Haley", 
            jersey_number = "10", id = "453d63d6-74ce-4169-9405-e0b2444e45f2"), .Names = c("full_name", 
        "jersey_number", "id")), structure(list(full_name = "Emmitt Matthews Jr.", 
            jersey_number = "11", id = "70f012ae-5deb-42da-9fff-94ac68ae36a7"), .Names = c("full_name", 
        "jersey_number", "id")), structure(list(full_name = "Lamont West", 
            jersey_number = "15", id = "b191d2a3-19d5-48ed-b4c4-a55b6e039d8d"), .Names = c("full_name", 
        "jersey_number", "id")))), .Names = c("name", "market", 
    "id", "players")), away = structure(list(name = "Cyclones", 
        market = "Iowa State", id = "0e4258fa-3154-4c16-b693-adecab184c6c", 
        players = list(structure(list(full_name = "Nick Weiler-Babb", 
            jersey_number = "1", id = "067af76a-e3d7-439e-8a8a-b662f3ec08de"), .Names = c("full_name", 
        "jersey_number", "id")), structure(list(full_name = "Lindell Wigginton", 
            jersey_number = "5", id = "5c09bc24-a9e0-427f-b611-70231a651bdf"), .Names = c("full_name", 
        "jersey_number", "id")), structure(list(full_name = "Talen Horton-Tucker", 
            jersey_number = "11", id = "7513239c-27a3-455c-a670-167b3f23a071"), .Names = c("full_name", 
        "jersey_number", "id")), structure(list(full_name = "Michael Jacobson", 
            jersey_number = "12", id = "0ad642b3-b38e-47f7-adac-34039306826f"), .Names = c("full_name", 
        "jersey_number", "id")), structure(list(full_name = "Tyrese Haliburton", 
            jersey_number = "22", id = "82cee878-39a7-4fd2-a38f-76be4dd4cea4"), .Names = c("full_name", 
        "jersey_number", "id")))), .Names = c("name", "market", 
    "id", "players"))), .Names = c("home", "away")), statistics = list(
        structure(list(type = "fieldgoal", made = TRUE, shot_type = "jump shot", 
            three_point_shot = TRUE, points = 3L, team = structure(list(
                name = "Cyclones", market = "Iowa State", id = "0e4258fa-3154-4c16-b693-adecab184c6c"), .Names = c("name", 
            "market", "id")), player = structure(list(full_name = "Talen Horton-Tucker", 
                jersey_number = "11", id = "7513239c-27a3-455c-a670-167b3f23a071"), .Names = c("full_name", 
            "jersey_number", "id"))), .Names = c("type", "made", 
        "shot_type", "three_point_shot", "points", "team", "player"
        )), structure(list(type = "assist", team = structure(list(
            name = "Cyclones", market = "Iowa State", id = "0e4258fa-3154-4c16-b693-adecab184c6c"), .Names = c("name", 
        "market", "id")), player = structure(list(full_name = "Tyrese Haliburton", 
            jersey_number = "22", id = "82cee878-39a7-4fd2-a38f-76be4dd4cea4"), .Names = c("full_name", 
        "jersey_number", "id"))), .Names = c("type", "team", 
        "player")))), .Names = c("id", "clock", "updated", "description", 
"event_type", "attribution", "location", "possession", "on_court", 
"statistics")))

该列表出了很多问题。从stackoverflow直接复制/粘贴到我的R控制台中不起作用...但是,当我将列表发布到R脚本中并运行该代码块时,将创建该列表。再次对庞大的数据表示歉意,但这对重现我的问题很有帮助...

从这里开始很简单。我通常使用以下函数取消嵌套任何JSON:

my.pbp.df <- my.pbp.data %>%
    purrr::map(unlist) %>%
    purrr::map(t) %>%
    purrr::map(as_tibble) %>%
    dplyr::bind_rows() %>%
    readr::type_convert() %>%
    as.data.frame()

...但是输出不正确。

我注意到的是,虽然map(unlist)将每个列表项转换为字符向量,而map(t)将每个列表项转换为矩阵,但是当我运行map(as_tibble)时,我丢失了该列矩阵中任何重复名称的项目的名称。我剩下的是一个填充有以下名称的数据框:

> colnames(this.periods.data)
 [1] "id"                                  "clock"                               "updated"                             "description"                        
 [5] "event_type"                          "attribution.name"                    "attribution.market"                  "attribution.id"                     
 [9] "attribution.team_basket"             "location.coord_x"                    "location.coord_y"                    "on_court.home.name"                 
[13] "on_court.home.market"                "on_court.home.id"                    "on_court.home.players.full_name"     "on_court.home.players.jersey_number"
[17] "on_court.home.players.id"            "V18"                                 "V19"                                 "V20"                                
[21] "V21"                                 "V22"                                 "V23"                                 "V24"                                
[25] "V25"                                 "V26"                                 "V27"                                 "V28"                                
[29] "V29"                                 "on_court.away.name"                  "on_court.away.market"                "on_court.away.id"                   
[33] "on_court.away.players.full_name"     "on_court.away.players.jersey_number" "on_court.away.players.id"            "V36"                                
[37] "V37"                                 "V38"                                 "V39"                                 "V40"                                
[41] "V41"                                 "V42"                                 "V43"                                 "V44"                                
[45] "V45"                                 "V46"                                 "V47"                                 "possession.name"                    
[49] "possession.market"                   "possession.id"                       "V30"                                 "V31"                                
[53] "V32"                                 "V48"                                 "V49"                                 "V50"                                
[57] "statistics.type"                     "statistics.made"                     "statistics.shot_type"                "statistics.team.name"               
[61] "statistics.team.market"              "statistics.team.id"                  "statistics.player.full_name"         "statistics.player.jersey_number"    
[65] "statistics.player.id"                "statistics.rebound_type"             "statistics.three_point_shot"         "statistics.points"                  
[69] "V62"                                 "V63"                                 "V64"                                 "V65"                                
[73] "V66"                                 "V67"                                 "V68"   

所有V#列名实际上都应具有列名。例如,V18-V29之所以这样重命名,是因为它们实际上具有与前3列on_court.home.players.full_name, on_court.home.players.jersey_number, on_court.home.players.id完全相同的名称。运行map(as_tibble)时,这些名称会丢失。

简而言之,有没有一种更好的方法可以将列表/ JSON转换为数据框而不丢失列名?

谢谢!

编辑:所需的输出只是一组完整的列名。 V18应该是on_court.home.players.full_name.2,V19应该是on_court.home.players.jersey_number.2,V20应该是on_court.home.players.id.2,依此类推。而不是自动转换为无意义的V18 ,V19,V20,V21,V22等列,如果自动添加.x, .y.2, .3, .4, etc.的重复列名称来做这些名称,那就太好了。

1 个答案:

答案 0 :(得分:3)

你可以做

df <- bind_rows(map(my.pbp.data, as.data.frame))

更简洁(多亏@RonakShah)

df <- map_dfr(my.pbp.data, as.data.frame)

检查列名给出

colnames(df)
#[1] "id"
#[2] "clock"
#[3] "updated"
#[4] "description"
#[5] "event_type"
#[6] "attribution.name"
#[7] "attribution.market"
#[8] "attribution.id"
#[9] "attribution.team_basket"
#[10] "location.coord_x"
#[11] "location.coord_y"
#[12] "on_court.home.name"
#[13] "on_court.home.market"
#[14] "on_court.home.id"
#[15] "on_court.home.players.full_name"
#[16] "on_court.home.players.jersey_number"
#[17] "on_court.home.players.id"
#[18] "on_court.home.players.full_name.1"
#[19] "on_court.home.players.jersey_number.1"
#[20] "on_court.home.players.id.1"
#[21] "on_court.home.players.full_name.2"
#[22] "on_court.home.players.jersey_number.2"
#[23] "on_court.home.players.id.2"
#[24] "on_court.home.players.full_name.3"
#[25] "on_court.home.players.jersey_number.3"
#[26] "on_court.home.players.id.3"
#[27] "on_court.home.players.full_name.4"
#[28] "on_court.home.players.jersey_number.4"
#[29] "on_court.home.players.id.4"
#[30] "on_court.away.name"
#[31] "on_court.away.market"
#[32] "on_court.away.id"
#[33] "on_court.away.players.full_name"
#[34] "on_court.away.players.jersey_number"
#[35] "on_court.away.players.id"
#[36] "on_court.away.players.full_name.1"
#[37] "on_court.away.players.jersey_number.1"
#[38] "on_court.away.players.id.1"
#[39] "on_court.away.players.full_name.2"
#[40] "on_court.away.players.jersey_number.2"
#[41] "on_court.away.players.id.2"
#[42] "on_court.away.players.full_name.3"
#[43] "on_court.away.players.jersey_number.3"
#[44] "on_court.away.players.id.3"
#[45] "on_court.away.players.full_name.4"
#[46] "on_court.away.players.jersey_number.4"
#[47] "on_court.away.players.id.4"
#[48] "possession.name"
#[49] "possession.market"
#[50] "possession.id"
#[51] "statistics.type"
#[52] "statistics.made"
#[53] "statistics.shot_type"
#[54] "statistics.team.name"
#[55] "statistics.team.market"
#[56] "statistics.team.id"
#[57] "statistics.player.full_name"
#[58] "statistics.player.jersey_number"
#[59] "statistics.player.id"
#[60] "statistics.rebound_type"
#[61] "statistics.three_point_shot"
#[62] "statistics.points"
#[63] "statistics.type.1"
#[64] "statistics.team.name.1"
#[65] "statistics.team.market.1"
#[66] "statistics.team.id.1"
#[67] "statistics.player.full_name.1"
#[68] "statistics.player.jersey_number.1"
#[69] "statistics.player.id.1"