2014-06-09 17 views
5

Tôi có đoạn mã sau:R Nhận tên cột từ 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 

Tất cả các dữ liệu được hiển thị.

Tôi làm cách nào để có được tất cả của data?

Tôi đã thử số này names(data) nhưng chỉ xuất ra "item" "item" "item".

Trả lời

5

Bạn có danh sách data.frames. Bạn có thể chèo ràng buộc chúng lại với nhau:

> 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 ... 

Nếu bạn chỉ muốn các chức danh

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

Cảm ơn. Đó là đủ đơn giản. Có cách nào khác để tránh ràng buộc các khung dữ liệu hay điều này là cần thiết không? – user1477388

+0

@ user1477388 bạn có thể phân tích cú pháp 'XML' và sử dụng' xpath' để lấy tiêu đề nếu tất cả bạn muốn – jdharrison

+0

Tôi chỉ tò mò về các phương pháp khác - cố gắng học R. Tôi tin rằng cách tiếp cận ràng buộc của bạn có lẽ là tốt nhất. – user1477388

3

Bạn cũng có thể truy xuất chỉ có tên mà không ràng buộc dữ liệu khung

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

Đây là cách tôi thường làm điều này - nhanh chóng và dễ dàng.

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 ... 

Bạn có thể truy cập bất kỳ yếu tố nào khác tương tự, ví dụ:

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

Ồ, điều đó rất đơn giản. Cảm ơn nhiều. – user1477388

+2

'unname' là tùy chọn - chỉ cần dọn dẹp đầu ra một chút :) – jbaums

Các vấn đề liên quan