2014-01-06 5 views
2

나는 lapply를 사용하고자하는 데이터 프레임을 가지고 있습니다. 여기 첫 번째 열의 첫 번째 값을 선택 :tm corpus 함수에 lapply 동작

link <- c(
    "http://www.r-statistics.com/tag/hadley-wickham/",              
    "http://had.co.nz/",                      
    "http://vita.had.co.nz/articles.html",                 
    "http://blog.revolutionanalytics.com/2010/09/the-r-files-hadley-wickham.html",       
    "http://www.analyticstory.com/hadley-wickham/" 
)    

agstudy]

create.corpus <- function(url.name){ 
    doc=htmlParse(link) 
    parag=xpathSApply(doc,'//p',xmlValue) 
    cc=Corpus(VectorSource(parag)) 
    meta(cc,type='corpus','link')=link 
    return(cc) 
} 

에 코퍼스으로 링크 저장을의 내용을 얻는다 [덕분에 적용 할 수있는 기능을하지만 얻을 수 없다 lapply를 통해 작동하는 함수 :

cc=lapply(link,create.corpus) # does not work 
cc=lapply(link,nchar) # works 

link=link[1] # try on single element 
cc=create.corpus(link) # works 

왜이 기능은 lapply에서 작동하지 않습니까?

답변

2

함수에 문제가 있습니다. link의 모든 인스턴스를 url.name으로 바꾸면 제대로 작동합니다.

# library(XML); library(tm) 

create.corpus <- function(url.name){ 
    doc=htmlParse(url.name) 
    parag=xpathSApply(doc,'//p',xmlValue) 
    cc=Corpus(VectorSource(parag)) 
    meta(cc,type='corpus','link') <- url.name 
    return(cc) 
} 

cc <- lapply(link, create.corpus) 

결과 :

> cc 
[[1]] 
A corpus with 48 text documents 

[[2]] 
A corpus with 2 text documents 

[[3]] 
A corpus with 41 text documents 

[[4]] 
A corpus with 25 text documents 

[[5]] 
A corpus with 39 text documents 
+0

감사합니다! 그것은 작동합니다. – Henk