2013-08-13 3 views
5

Mam kod html i R jak te i trzeba powiązać każdą wartość węzła z jej identyfikatorem nadrzędnym w data.frame. Dla każdej osoby dostępne są różne informacje.R: Jak uzyskać atrybuty nadrzędne i wartości węzłów w czasie witryny?

example <- "<div class='person' id='1'> 
<div class='phone'>555-5555</div> 
<div class='email'>[email protected]</div> 
</div> 
<div class='person' id='2'> 
<div class='phone'>123-4567</div> 
<div class='email'>[email protected]</div> 
</div> 
<div class='person' id='3'> 
<div class='phone'>987-6543</div> 
<div class='age'>32</div> 
<div class='city'>New York</div> 
</div>" 

doc = htmlTreeParse(example, useInternalNodes = T) 

values <- xpathSApply(doc, "//*[@class='person']/div", xmlValue) 
variables <- xpathSApply(doc, "//*[@class='person']/div", xmlGetAttr, 'class') 
id <- xpathSApply(doc, "//*[@class='person']", xmlGetAttr, 'id') 

# The problem: create a data.frame(id,variables,values) 

Z xpathSApply(), mogę dostać wartości telefonu, e-mail i wiek, a także atrybuty osoba (ID) też. Jednak te informacje są izolowane i muszę je odnieść do właściwej zmiennej data.frame i właściwej osoby. W moich prawdziwych danych jest wiele różnych informacji, więc ten proces nazywania każdej zmiennej musi być automatyczny.

Moim celem jest utworzenie elementu data.frame, który odpowiada każdemu idowi odpowiednich danych.

id variables   values 
1 1  phone  555-5555 
2 1  email [email protected] 
3 2  phone  123-4567 
4 2  email [email protected] 
5 3  phone  987-6543 
6 3  age    32 
7 3  city  New York 

wierzę, że będę musiał utworzyć funkcję używać wewnątrz xpathSApply który dostanie w tym samym czasie przez telefon osoba i osoba id, więc będą one powiązane, ale nie miałem żadnych sukcesów z do tej pory.

Czy ktoś może mi pomóc?

Odpowiedz

7

W ogóle jej nie będzie łatwe:

idNodes <- getNodeSet(doc, "//div[@id]") 
ids <- lapply(idNodes, function(x) xmlAttrs(x)['id']) 
values <- lapply(idNodes, xpathApply, path = './div[@class]', xmlValue) 
attributes <- lapply(idNodes, xpathApply, path = './div[@class]', xmlAttrs) 
do.call(rbind.data.frame, mapply(cbind, ids, values, attributes)) 
    V1    V2 V3 
1 1  555-5555 phone 
2 1 [email protected] email 
3 2  123-4567 phone 
4 2 [email protected] email 
5 3  987-6543 phone 
6 3    32 age 
7 3  New York city 

Powyższy da atrybutu i pary wartości assumming są zagnieżdżone w div z towarzyszącym id.

UPDATE: jeśli chcesz zawinąć go w typ połączenia xpathApply

utilFun <- function(x){ 
    id <- xmlGetAttr(x, 'id') 
    values <- sapply(xmlChildren(x, omitNodeTypes = "XMLInternalTextNode"), xmlValue) 
    attributes <- sapply(xmlChildren(x, omitNodeTypes = "XMLInternalTextNode"), xmlAttrs) 
    data.frame(id = id, attributes = attributes, values = values, stringsAsFactors = FALSE) 
} 
res <- xpathApply(doc, '//div[@id]', utilFun) 
do.call(rbind, res) 
    id attributes   values 
1 1  phone  555-5555 
2 1  email [email protected] 
3 2  phone  123-4567 
4 2  email [email protected] 
5 3  phone  987-6543 
6 3  age    32 
7 3  city  New York 
Powiązane problemy