# loop operation on a list

dod=c(1,2,3,4)
doe=c(4,3,5,1)

dof=list(dod, dow)

I want to write a function that compares the vectors in the list and returns which of the vectors has the greater sum.

Here are two solutions. One uses nested function calls and the second has a user defined function.

``````dod=c(1,2,3,4)
doe=c(4,3,5,1)

dof=list(dod, doe)

#nested function calls
dof[[which.max(sapply(dof,sum))]]
#>  4 3 5 1

#user defined function
GetLargest <- function(L) {
SUMS <- sapply(L, sum)
Biggest <- which.max(SUMS)
L[[Biggest]]
}
GetLargest(dof)
#>  4 3 5 1
``````

Created on 2022-07-09 by the reprex package (v0.2.1)

1 Like

Thanks .
instead I want the output to be with doe.

lets say
library(igraph)

cnodes1=c(2,3,4,6)
cnodes2=c(1,57)
nodes=list(cnodes1, cnodes 2)
graph=g=graph_from_atlas(500)

#I would like to use the nodes to determine which would derive the highest sub-edges

v = V(g)[cnodes1]
E1 = which(sapply(E(g), function(e) ends(g, e)) %in% v)
E2 = which(sapply(E(g), function(e) ends(g, e)) %in% v)
Internal = intersect(E1, E2)
len=ends(g, Internal)
nrow(len)

v = V(g)[cnodes2]
E1 = which(sapply(E(g), function(e) ends(g, e)) %in% v)
E2 = which(sapply(E(g), function(e) ends(g, e)) %in% v)
Internal = intersect(E1, E2)
len2=ends(g, Internal)
nrow(len2)

if ((nrow(len2) > nrow(len1)) {
ans= cnodes2
}
else{
ans= cnodes1
}
return (ans)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.