2010-08-04 8 views
3

아직까지는 묻지는 않았지만 목록의 정보를 여러 수준 및 고르지 않은 구조와 결합하여 "긴"형식의 데이터 프레임으로 만들 수 있습니까?고르지 않은 계층 구조 목록을 데이터 프레임으로 변환

은 특히 : 함께,

> llply(xml.list, function(x) llply(x, function(x) table(names(x)))) 
$top 
$top$atbat 
.attrs pitch 
    1  4 
$top$atbat 
.attrs pitch 
    1  4 
$top$atbat 
.attrs pitch 
    1  5 
$bottom 
$bottom$action 
    b des event  o pitch player  s 
    1  1  1  1  1  1  1 
$bottom$atbat 
.attrs pitch 
    1  5 
$bottom$atbat 
.attrs pitch 
    1  5 
$bottom$atbat 
.attrs pitch runner 
    1  5  1 
$bottom$atbat 
.attrs pitch runner 
    1  7  1 
$.attrs 
$.attrs$num 
character(0) 
$.attrs$away_team 
character(0) 
$.attrs$ 

내가 가지고 싶은 것은 피치 범주에서 명명 된 벡터에서 데이터 프레임은 다음

library(XML) 
library(plyr) 
xml.inning <- "http://gd2.mlb.com/components/game/mlb/year_2009/month_05/day_02/gid_2009_05_02_chamlb_texmlb_1/inning/inning_5.xml" 
xml.parse <- xmlInternalTreeParse(xml.inning) 
xml.list <- xmlToList(xml.parse) 
## $top$atbat 
## $top$atbat$pitch 
##    des    id   type    x    y 
##   "Ball"   "310"    "B"   "70.39"  "125.20" 

는 구조입니다 (상단, atbat, 하단). 따라서 다른 수의 열로 인해 data.frame에 맞지 않는 레벨은 무시해야합니다. 다음과 같은 내용이 있습니다.

first second third des  x 
1 top atbat pitch Ball 70.29 
2 top atbat pitch Strike 69.24 
3 bottom atbat pitch Out 67.22 

우아한 방법이 있나요? 감사!

+0

관련 질문 : http://stackoverflow.com/questions/2067098/how-to-transform-xml-data-into-a-data-frame – apeescape

답변

5

우아한 것에 대해서는 잘 모릅니다. 그러나 이것은 효과적입니다. plyr에 익숙한 사람들은 좀 더 일반적인 해결책을 제공 할 수 있습니다.

cleanFun <- function(x) { 
    a <- x[["atbat"]] 
    b <- do.call(rbind,a[names(a)=="pitch"]) 
    c <- as.data.frame(b) 
} 
ldply(xml.list[c("top","bottom")], cleanFun)[,1:5] 
    .id    des id type  x 
1 top   Ball 310 B 70.39 
2 top Called Strike 311 S 118.45 
3 top Called Strike 312 S 86.70 
4 top In play, out(s) 313 X 79.83 
5 bottom   Ball 335 B 15.45 
6 bottom Called Strike 336 S 77.25 
7 bottom Swinging Strike 337 S 99.57 
8 bottom   Ball 338 B 106.44 
9 bottom In play, out(s) 339 X 134.76 
1

ldply().id 기능은 좋은,하지만 당신은 다른 ldply()을하면 그들이 중복처럼 보인다. 우리가 이미 .id 있었기 때문에

aho <- ldply(llply(xml.list[[1]], function(x) ldply(x, function(x) rbind.fill(data.frame(t(x)))))) 
> aho[1:5,1:4] 
    .id              des id type 
1 pitch              Ball 310 B 
2 pitch            Called Strike 311 S 
3 pitch            Called Strike 312 S 
4 pitch           In play, out(s) 313 X 
5 .attrs Alexei Ramirez lines out to second baseman Ian Kinsler. <NA> <NA> 

ldply().id 누락 : 여기

상당히 rbind.fill() 사용 일반 함수이다. 우리는 첫 번째 .id을 다른 이름으로 지정하여이 문제를 해결할 수 있지만 일관된 것 같지 않습니다.

aho2 <- ldply(llply(xml.list[[1]], function(x) { 
    out <- ldply(x, function(x) rbind.fill(data.frame(t(x)))) 
    names(out)[1] <- ".id2" 
    out 
})) 
> aho2[1:5,1:4] 
    .id .id2              des id 
1 atbat pitch              Ball 310 
2 atbat pitch            Called Strike 311 
3 atbat pitch            Called Strike 312 
4 atbat pitch           In play, out(s) 313 
5 atbat .attrs Alexei Ramirez lines out to second baseman Ian Kinsler. <NA>