将包含列表的R数据帧列转换为数据框列

时间:2015-11-25 03:44:44

标签: json r nested dataframe

我有一个讨厌的嵌套JSON文件,我想从中提取数据的特定部分到数据框中。我已经能够将我感兴趣的数据放入数据框中,但第一列包含嵌套列表。

       PlayerStats.PlayerStat                               X.attributes.id X.attributes.player_name X.attributes.position X.attributes.position_id
1  list('@value'=c("","","",...), '@attribute'=c("","","",...)) 8683                 John Smith                Back 7                       15
2  list('@value'=c("","","",...), '@attribute'=c("","","",...)) 19104              Tony Fratone                Back 6                       14
3  list('@value'=c("","","",...), '@attribute'=c("","","",...)) 10091              Ryan Elliott                Back 5                       13

理想情况下,我想将'@attribute'列表转换为数据框列。首先,我不确定如何访问每个列表以对它们执行操作。此外,“@”似乎也在为这项工作带来麻烦。

我试过这个:

matchData <- fromJSON("json_data.json", flatten = TRUE)

# Get player stats for team 1
team1 <- data.frame(matchData$TeamDetail$Team$Player[1], stringsAsFactors = FALSE)

pStats <- (team1$PlayerStats.PlayerStat)

stats <- as.data.frame(do.call(cbind, pStats))

但最终会为每个列值创建一个新行。

我正撞在墙上(也许是因为睡眠不足) - 任何指导都会受到赞赏。

这是JSON的片段,代表一个玩家的统计数据:

"Team": [{
                "Player": [{
                    "PlayerStats": {
                        "PlayerStat": [{
                            "@value": "",
                            "@attributes": {
                                "restart_opp_player": "0"
                            }
                        },
                        {
                            "@value": "",
                            "@attributes": {
                                "kick_oppn_collection": "0"
                            }
                        },
                        {
                            "@value": "",
                            "@attributes": {
                                "kicks_from_hand": "4"
                            }

                        ...

                    ]
                },
                "@attributes": {
                    "id": "7654",
                    "player_name": "Tom Smith",
                    "position": "Back 7",
                    "position_id": "15"
                }
            },

这是dput(team1$PlayerStats.PlayerStat)输出的一部分(对于大量数据抱歉):

    `@attributes.kick_charged_down` = c(NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "0", NA, 
NA, NA, NA, NA, NA, NA, NA, NA), `@attributes.lineout_throw_won_penalty` = c(NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "0", 
NA, NA, NA, NA, NA, NA, NA, NA), 
...
, `@attributes.runs` = c(NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, "5", NA, NA), `@attributes.goals` = c(NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, "0", NA), `@attributes.penalty_conceded_high_tackle` = c(NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, "0")), .Names = c("@value", "@attributes.restart_opp_player", 
"@attributes.kick_oppn_collection", "@attributes.kicks_from_hand", 
"@attributes.penalty_conceded_killing_ruck", "@attributes.penalty_conceded_own_half", 
"@attributes.kick_penalty_good", "@attributes.lineouts_won", 
"@attributes.kick_in_field", "@attributes.rucks_won", "@attributes.minutes_played_second_half", 
"@attributes.metres", "@attributes.scrums_lost_reversed", "@attributes.tries", 
"@attributes.lineout_won_opp_throw", "@attributes.lineouts_infringe_opp", 
"@attributes.scrums_won_penalty_try", "@attributes.turnover_turnover_forward_pass", 
"@attributes.scrums_won_free_kick", "@attributes.scrums_won_pushover_try", 
"@attributes.lineouts_lost", "@attributes.turnover_own_half", 
"@attributes.dropped_catch", "@attributes.missed_tackles", "@attributes.scrums_lost_outright", 
"@attributes.collection_loose_ball", "@attributes.restart_error_not_ten", 
"@attributes.scrums_lost_penalty", "@attributes.missed_conversion_goals", 
"@attributes.pc_kick_percent", "@attributes.kicks", "@attributes.restarts_lost", 
"@attributes.pen_defs", "@attributes.missed_goals", "@attributes.lineout_won_steal", 
"@attributes.minutes_played_before_second_half_extra", "@attributes.penalty_conceded_foul_play", 
"@attributes.penalty_goals", "@attributes.lineout_throw_lost_handling_error", 
"@attributes.kick_penalty_bad", "@attributes.kick_percent_success", 
"@attributes.lineout_throw_won_clean", "@attributes.restart_opp_error", 
"@attributes.drop_goals_converted", "@attributes.conversion_goals", 
"@attributes.points", "@attributes.mauls_won_outright", "@attributes.clean_breaks", 
"@attributes.collection_failed", "@attributes.kick_in_touch", 
"@attributes.handling_error", "@attributes.carries_support", 
"@attributes.kick_possession_retained", "@attributes.scrums_lost_free_kick", 
"@attributes.id", "@attributes.penalty_conceded_dissent", "@attributes.try_assist", 
"@attributes.defenders_beaten", "@attributes.penalty_conceded_lineout_offence", 
"@attributes.catch_from_kick", "@attributes.scrums_won_penalty", 
"@attributes.minutes_played_first_half_extra", "@attributes.collection_from_kick", 
"@attributes.restart_own_player", "@attributes.penalty_conceded_line_out_offence", 
"@attributes.penalty_conceded_collapsing_maul", "@attributes.lineout_success", 
"@attributes.penalty_conceded_offside", "@attributes.collection_success", 
"@attributes.turnover_carried_in_touch", "@attributes.player_id", 
"@attributes.restarts_success", "@attributes.rucks_lost", "@attributes.turnover_bad_pass", 
"@attributes.mauls_won_try", "@attributes.ball_out_of_play", 
"@attributes.lineout_non_straight", "@attributes.turnover_carried_over", 
"@attributes.lineout_throw_won_tap", "@attributes.scrums_won_outright", 
"@attributes.turnover_lost_in_ruck_or_maul", "@attributes.tackle_success", 
"@attributes.kick_out_of_play", "@attributes.kick_possession_lost", 
"@attributes.carries_not_made_gain_line", "@attributes.missed_penalty_goals", 
"@attributes.yellow_cards", "@attributes.offload", "@attributes.retained_kicks", 
"@attributes.passes", "@attributes.carries_crossed_gain_line", 
"@attributes.penalty_conceded_delib_knock_on", "@attributes.lineouts_to_own_player", 
"@attributes.team_id", "@attributes.penalty_conceded_opp_half", 
"@attributes.restart_halfway", "@attributes.lineout_throw_lost_free_kick", 
"@attributes.minutes_played_before_penalty_shootOut", "@attributes.penalty_conceded_other", 
"@attributes.mauls_won", "@attributes.turnover_forward_pass", 
"@attributes.penalty_conceded_scrum_offence", "@attributes.tackles", 
"@attributes.pen_offs", "@attributes.lineout_throw_lost_not_straight", 
"@attributes.try_kicks", "@attributes.mauls_lost_turnover", "@attributes.penalties_conceded", 
"@attributes.mauls_won_penalty", "@attributes.lineout_won_own_throw", 
"@attributes.mauls_won_penalty_try", "@attributes.minutes_played_before_second_half", 
"@attributes.lineout_throw_lost_outright", "@attributes.restart_22m", 
"@attributes.mauls_lost_outright", "@attributes.mauls_lost", 
"@attributes.penalty_conceded_collapsing_offence", "@attributes.minutes_played_first_half", 
"@attributes.turnover_opp_half", "@attributes.turnover_knock_on", 
"@attributes.minutes_played_before_first_half_extra", "@attributes.game_id", 
"@attributes.turnover_won", "@attributes.penalty_conceded_obstruction", 
"@attributes.penalty_conceded_stamping", "@attributes.restart_error_out_of_play", 
"@attributes.kick_metres", "@attributes.collection_interception", 
"@attributes.pickup", "@attributes.minutes_played_second_half_extra", 
"@attributes.penalty_conceded_handling_in_ruck", "@attributes.try_assists", 
"@attributes.minutes_played_total", "@attributes.penalty_conceded_early_tackle", 
"@attributes.lineout_throw_lost_penalty", "@attributes.minutes_played_before_first_half", 
"@attributes.carries_metres", "@attributes.true_retained_kicks", 
"@attributes.bad_passes", "@attributes.kick_try_scored", "@attributes.turnovers_conceded", 
"@attributes.red_cards", "@attributes.kick_touch_in_goal", "@attributes.kick_charged_down", 
"@attributes.lineout_throw_won_penalty", "@attributes.penalty_conceded_wrong_side", 
"@attributes.total_lineouts", "@attributes.gain_line", "@attributes.lineout_throw_won_free_kick", 
"@attributes.restarts_won", "@attributes.runs", "@attributes.goals", 
"@attributes.penalty_conceded_high_tackle"), class = "data.frame", row.names = c(NA, 
152L)))

1 个答案:

答案 0 :(得分:0)

以下代码会将第一行的Playerstats转换为数据框:

pStats <- data.frame(team1$PlayerStats.PlayerStat[1])

以下代码将遍历PlayerStats中的所有行,并使用foreach包结合所有结果

require(foreach)

pStats<-  foreach(i=1:dim(team1)[1], .combine=rbind) %do% {
    pStats <- data.frame(team1$PlayerStats.PlayerStat[i])
    }

关于如何处理和@:

example <- list('@value'=c("A","B","C"), '@attribute'=c("1","2","3"))

使用example$'@value'example$'@attribute',您可以使用标签

访问数据

广告下面的代码将生成和data.frame df.example <- data.frame(example)