R从data.frame获取列名

use*_*388 5 xml r

我有以下代码:

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
Run Code Online (Sandbox Code Playgroud)

输出所有数据.

我怎样才能得到所有title的的data

我试过这个,names(data)但它只输出"item" "item" "item".

jdh*_*son 5

您有一个data.frames列表.您可以将它们绑定在一起:

> 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 ...
Run Code Online (Sandbox Code Playgroud)

如果你只想要标题

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"     
Run Code Online (Sandbox Code Playgroud)

  • 有很多方法可以从"XML"中获取数据.主要取决于`XML`的复杂性.我最喜欢的答案之一是http://stackoverflow.com/questions/22643580/combine-values-in-huge-xml-files (2认同)