【问题标题】:bind character vector to list into dataframe将字符向量绑定到数据框中
【发布时间】:2014-01-03 17:28:40
【问题描述】:

我有一个 URL 列表,并提取了如下内容:

library(httr)
link="http://www.workerspower.net/disposable-workers-the-real-price-of-sweat-shop-labor"
get.link=GET(link)
get.content=content(x2,as="text")
extract.content=str_extract_all(y2,"<p>(.*?)</p>")

这给出了一个带有文本的“1 列表”。每个列表的长度取决于/随 URL 而变化。 我想将 URL [link] 与内容 [extract.content] 绑定并将其转换为数据框,然后将其导入语料库。 我的尝试失败了,例如。由于行长不同,这不起作用:

all=data.frame(url.vec=c(link1,link2),text.vec=c(extract.content1,extract.content2))

有谁知道如何将字符[向量]与字符[列表]结合起来?

【问题讨论】:

    标签: r base tm httr


    【解决方案1】:

    我会使用XML 包来做到这一点。那么你应该避免在 html/xml 文档中使用正则表达式。请改用xpath。在这里,我创建了一个小函数,它提供了一个链接,它创建了语料库。

    library(XML)
    create.corpus <- function(link){
      doc <- htmlParse(link)
      parag <- xpathSApply(doc,'//p',xmlValue)
      library(tm)
      cc <- Corpus(VectorSource(parag))
      meta(cc,type='corpus','link') <- link
      cc
    }
    ## call it 
    cc <- create.corpus(link)
    

    检查结果:

     meta(cc,type='corpus')
    # $create_date
    # [1] "2014-01-03 17:40:50 GMT"
    # 
    # $creator
    # [1] ""
    # 
    # $link
    # [1] "http://www.workerspower.net/disposable-workers-the-real-price-of-sweat-shop-labor"
    
    > cc
    # A corpus with 36 text documents
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-20
    • 2013-05-21
    • 2017-07-07
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 2018-12-05
    • 1970-01-01
    相关资源
    最近更新 更多