2014-06-09 18 views
5

Mam następujący kod:R Get nazwy kolumn z data.frame

install.packages("XML") 
library(XML) 
install.packages("plyr") 
library(plyr) 

feed <- "http://feeds.reuters.com/Reuters/worldNews?format=xml" 
reuters<-xmlToList(feed) 
data <- lapply(reuters[[1]][names(reuters[[1]])=="item"], data.frame) 

data 

Wszystkie dane jest wyjście.

Jak mogę uzyskać wszystkie title z data?

Próbowałem tego names(data), ale wyprowadza tylko "item" "item" "item".

Odpowiedz

5

Masz listę data.frames. Można wiosłować wiążą je ze sobą:

> names(do.call(rbind.data.frame, data)) 
[1] "title"   "link"   "description"  "category.text" 
[5] "category..attrs" "pubDate"   "guid.text"  "guid..attrs"  
[9] "origLink" 

data1 <- do.call(rbind.data.frame, data) 
> head(data1$title) 
[1] Niger says will repatriate its illegal migrants from Algeria  
[2] Twin bombing near Kurdish party office in north Iraq kills 30  
[3] Suicide bomber kills four soldiers in Pakistan's tribal northwest 
[4] Sisi keeps Egyptian premier to fix economy after turmoil   
[5] Kosovo's Thaci has tough job to form new cabinet, keep promises 
[6] Libyan Supreme Court rules PM's election unconstitutional   
25 Levels: Niger says will repatriate its illegal migrants from Algeria ... 

Jeśli chcesz tylko tytuły

xData <- xmlParse(feed) 
> head(xpathSApply(xData, "//title", xmlValue)) 
[1] "Reuters: World News"             
[2] "Reuters: World News"             
[3] "South Africa platinum strike talks in crucial final day of mediation" 
[4] "Africa's sports bars, TV shacks step up security for World Cup"  
[5] "Niger says will repatriate its illegal migrants from Algeria"   
[6] "Twin bombing near Kurdish party office in north Iraq kills 30"  
+0

Dzięki. To dość proste. Czy jest jakaś alternatywa dla uniknięcia wiązania ramek danych lub czy jest to niezbędne? – user1477388

+0

@ user1477388 możesz po prostu sparsować 'XML' i użyć' xpath', aby uzyskać tytuły, jeśli tylko chcesz – jdharrison

+0

Po prostu ciekawi mnie inne metody - próbuję się uczyć R. Wierzę, że twoje wiążące podejście jest prawdopodobnie najlepsze. – user1477388

3

Można również retreive tylko nazwy bez wiązania danych ramki

Titles <- character(length(data)) 
for (i in seq_len(length(data))) Titles[i] <- as.character(data[[i]]$title) 
Titles 
[1] "Niger says will repatriate its illegal migrants from Algeria"      "Twin bombing near Kurdish party office in north Iraq kills 30"     
[3] "Suicide bomber kills four soldiers in Pakistan's tribal northwest"    "Sisi keeps Egyptian premier to fix economy after turmoil"       
[5] "Kosovo's Thaci has tough job to form new cabinet, keep promises"     "Libyan Supreme Court rules PM's election unconstitutional"      
[7] "Thai junta to explain itself to international rights groups"      "Well-trained and armed, Taliban tried to hijack plane in Pakistan"    
[9] "Russia would react to NATO build-up near borders: minister"      "Myanmar military 'tortures civilians': human rights group"      
[11] "Five jailed for killing Russia's Politkovskaya, mastermind unknown" 
... 
3

Oto jak zwykle zrób to - szybko i łatwo.

unname(sapply(data, '[[', 'title')) 

# [1] South Africa platinum strike talks in crucial final day of mediation 
# [2] Africa's sports bars, TV shacks step up security for World Cup  
# [3] Niger says will repatriate its illegal migrants from Algeria   
# [4] Twin bombing near Kurdish party office in north Iraq kills 30  
# [5] Suicide bomber kills four soldiers in Pakistan's tribal northwest 
# [6] Sisi keeps Egyptian premier to fix economy after turmoil    
# 25 Levels: South Africa platinum strike talks in crucial final day of mediation ... 

Można uzyskać dostęp do dowolnego z pozostałych elementów w podobny sposób, np.

unname(sapply(data, '[[', 'link')) 
+0

To bardzo proste. Dzięki wielkie. – user1477388

+2

"Nienazwanie" jest opcjonalne - po prostu trochę upraszcza wyjście :) – jbaums