2015-11-25 193 views
0

我有一個討厭的嵌套JSON文件,我想從中提取特定部分的數據到數據框中。我已經能夠將我感興趣的數據放入數據框中,但第一列包含一個嵌套列表。將包含列表的R數據幀列轉換爲數據幀列

 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))) 
+3

你能分享一下你的JSON文件嗎?否則,我們都在猜測你在做什麼。 – thelatemail

+1

你可以添加dput(team1 $ PlayerStats.PlayerStat)或dput(head(team1))的結果嗎? –

+0

我已經添加了JSON文件的片段,以及來自'dput'的片段。 – user256599

回答

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'您可以通過使用標籤訪問數據

廣告folowwing代碼將生成和data.frame df.example <- data.frame(example)

+0

謝謝@Berecht - 這似乎產生了與以前相同的輸出 - 每行一個值,導致3496行,而不是23. – user256599

+0

通過更改foreach .combine語句cbind,您可以按列連接。請注意,組合data.frame的長度具有相同的長度。 – Berecht

+0

@ user256599 - 通過更改foreach .combine語句cbind,您可以按列連接。請注意,組合data.frame的長度具有相同的長度。以下原則:'stats < - cbind(data.frame(team1 $ PlayerStats.PlayerStat [1]),data.frame(team1 $ PlayerStats.PlayerStat [2]))' – Berecht