R中循环的矢量化

R中循环的矢量化,r,for-loop,vectorization,pubmed,rentrez,R,For Loop,Vectorization,Pubmed,Rentrez,哦,老兄。我很难从代码中删除循环,因为我发现它们是如此直观,我最初学会了C++。下面,我获取用于搜索的ID(本例中为copd),并使用该ID检索其完整的XML文件,并从中将其位置保存到向量中。我不知道如何加快搜索速度,在700个ID上运行大约需要5分钟,而大多数搜索都有70000多个ID。感谢您的指导 library(rentrez) library(XML) # number of articles for term copd count <- entrez_search(db = "

哦,老兄。我很难从代码中删除循环,因为我发现它们是如此直观,我最初学会了C++。下面,我获取用于搜索的ID(本例中为copd),并使用该ID检索其完整的XML文件,并从中将其位置保存到向量中。我不知道如何加快搜索速度,在700个ID上运行大约需要5分钟,而大多数搜索都有70000多个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)
}
库(rentrez)
库(XML)
#长期慢性阻塞性肺病的文章数量

计数这可能会给你一个开始-似乎有可能一次拉下多个

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))
库(rentrez)
库(xml2)
#长期慢性阻塞性肺病的文章数量

计数这可能会给你一个开始-似乎有可能一次拉下多个

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))
库(rentrez)
库(xml2)
#长期慢性阻塞性肺病的文章数量

count不能同时获得整个向量吗?为什么不使用循环?您的代码效率很低,但在本例中它不是瓶颈。不管怎样,现在R中的循环实际上是不错的。@PaulBailey如果他每次都必须单独提出请求,这实际上是一个瓶颈。
entrez_fetch
不能一次给出整个向量吗?你为什么不想使用循环呢?您的代码效率很低,但在本例中它不是瓶颈。不管怎么说,现在R中的循环实际上是相当不错的。@PaulBailey如果他每次都要提出单独的请求,这实际上是一个瓶颈。