- Is there a more efficient method of doing this?
I think so. Do not call V(g)
all the time, but put the attribute in a vector, and index it. If you include some example data, then I'll also include some code.
- Could this fall under a page rank type algorithm using the x value instead of degree
No, PageRank is recursive, your rank depends on the whole network, not only on the score of your neighbors.
- Would it be possible to use a GPU somehow?
Not with igraph. Put you can certainly make this fast enough without the GPU, so I would not go that way.
- Would this be quicker in igraph Python?
Depends how you write it. If you write it the correct way in R, then it will not be faster in Python, either, imo.
EDIT:
I left out the progress bar, because that's the slowest, actually.
Fastest solution above with data frame
system.time({
sub_ages2 <- data.frame(row.names = V(g)$NAME, X = V(g)$X);
avg_contact_ages <- ldply(adjcency_list, function(neis) {
mean(sub_ages2[neis, "X"], na.rm = T)
})
})
# user system elapsed
# 0.368 0.020 0.386
Slightly faster with sapply
system.time({
sub_ages2 <- data.frame(row.names = V(g)$NAME, X = V(g)$X);
avg_contact_ages <- sapply(adjcency_list, function(neis) {
mean(sub_ages2[neis, "X"], na.rm = TRUE)
})
})
# user system elapsed
# 0.340 0.017 0.356
Using factors
system.time({
adj_vec <- unlist(adjcency_list)
adj_fac <- factor(rep(seq_along(adjcency_list),
sapply(adjcency_list, length)),levels=seq_len(vcount(g)))
avg_contact_ages <- tapply(V(g)$X[adj_vec], adj_fac, mean, na.rm=TRUE)
})
# user system elapsed
# 0.131 0.008 0.138
If you need more speedup, you'll probably need to go to C/C++, Rcpp would be a relatively easy solution.