[英]Vectorizing for-loop in R
天啊。 我非常討厭從代碼中刪除for循環,因為我發現它們如此直觀,並且我首先學習了C ++。 在下面,我要獲取ID進行搜索(在本例中為copd),並使用該ID檢索其完整的XML文件,並從中將其位置保存到向量中。 我不知道如何加快速度,使用700個ID花費了大約5分鍾的時間,而大多數搜索都具有70,000多個ID。 感謝您的指導。
library(rentrez)
library(XML)
# number of articles for term copd
count <- entrez_search(db = "pubmed", term = "copd")$count
# set max to count
id <- entrez_search(db = "pubmed", term = "copd", retmax = count)$ids
# empty vector that will soon contain locations
location <- character()
# get all location data
for (i in 1:count)
{
# get ID of each search
test <- entrez_fetch(db = "pubmed", id = id[i], rettype = "XML")
# convert to XML
test_list <- XML::xmlToList(test)
# retrieve location
location <- c(location, test_list$PubmedArticle$MedlineCitation$Article$AuthorList$Author$AffiliationInfo$Affiliation)
}
這可能會給您一個開始-似乎可以一次下拉多個。
library(rentrez)
library(xml2)
# number of articles for term copd
count <- entrez_search(db = "pubmed", term = "copd")$count
# set max to count
id_search <- entrez_search(db = "pubmed", term = "copd", retmax = count, use_history = T)
# get all
document <- entrez_fetch(db = "pubmed", rettype = "XML", web_history = id_search$web_history)
document_list <- as_list(read_xml(document))
問題在於這仍然很耗時,因為有大量的文檔。 它也很好奇,當我嘗試過此操作時,它只能返回10,000篇文章-一次返回的內容可能有限制。
然后,您可以使用諸如purrr
包之類的東西開始提取所需的信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.