最近在写一个用程序查找pubmed文献的小程序,在执行代码的时候发现代码速度很慢(源代码用了for 循环),请问大家怎么进行向量化编程?
所需的包如下:RISmed
源代码如下:library(RISmed)
#x is file name of cpg_gene information
search_file_function <- function(x){
methylation_file <- read.csv(x, sep = ",", na.strings = "", stringsAsFactors = FALSE)
Kcpg_num <- nrow(methylation_file)
for (i in 1:Kcpg_num){
if(!is.na(methylation_file[i,"CpG.names"])){
search_topic <- paste("methylation", "AND", "cord", "blood", "AND", methylation_file[i,"CpG.names"], sep = " ")
search_query <- EUtilsSummary(search_topic)
records <- EUtilsGet(search_query)
pubmed_data <- data.frame("Title" = ArticleTitle(records),"Abstract" = AbstractText(records))
}
if (nrow(pubmed_data) != 0){
write.csv(pubmed_data, file = paste("data/search_file",methylation_file[i,"CpG.names"],".csv",sep = ""))
}
}
}