我有一个返回名为readFn()的igraph对象的函数。我想使用for循环和存储结果
来调用此函数files <- c("345","456")
for(i in files){
g <- readFn(i)
}
#Error: object 'g' not found
#In addition: There were 50 or more warnings (use warnings() to see the first #50)
readFn <- function(i) {
#some lines
graph #graph -> is an igraph object to be returned to for loop
}
我想在名为g_345和g_456
的igraph对象中获得2个图形答案 0 :(得分:1)
使用 lapply :
library(igraph)
# example function that returns igraph object
readFn <- function(i) {
graph.empty(n = 2)
}
files <- c("345","456")
g <- lapply(files , readFn)
# assign names
names(g) <- paste0("g_", files)
然后我们可以按名称访问每个图:
g$g_345
# IGRAPH 598e70c D--- 2 0 --
# + edges from 598e70c: