# strength returns NAs, when used in for loop.

**URL:** <https://igraph.discourse.group/t/strength-returns-nas-when-used-in-for-loop/1228>\
**Category:** Usage\
**Tags:** R\
**Created:** [23 May 2022 13:30 UTC](https://igraph.discourse.group/t/strength-returns-nas-when-used-in-for-loop/1228 "2022-05-23T13:30:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![idado6](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/idado6/32/766_2.png) [@idado6](https://igraph.discourse.group/u/idado6)\
**Post date:** [23 May 2022 13:30 UTC](https://igraph.discourse.group/t/strength-returns-nas-when-used-in-for-loop/1228/1 "2022-05-23T13:30:58Z")

</div>

Hello!  
for some reason, strength(g) returns NAs when is used in permutation test. In each iteration an induced\_subgraph is produced. then degree and strength for all nodes are calculated. The script worked well before but not anymore. I dont know why and If you have any way you can help or guide me to resolve this it would be much appreciated!

The script:

```r
#Iterating
for (i in c(1:iterations)){

#Sub-network:
# 1.1 sample the feature table n from each group.
features.subset.2 <- features %>% 
  group_by(core) %>% #Group by core \ no core
  sample_n(n, replace = F) #Sample eachgroup n times with no replacment

# 1.2 use the names of the genomes (nodes) to sample the original graph object to create an induced sub-graph.
#sub.g1 <- induced_subgraph(g1, which(V(g1)$name %in% features.subset.2$sample)) %>%
 # set_vertex_attr("Label", value = ifelse(features.subset.2$core == 1,"Core","NonCore")) #Set core\noncore attributes
sub.g1 <- igraph::induced_subgraph(g1, which(V(g1)$name %in% features.subset.2$sample)) %>%
  set_vertex_attr("Label", value = ifelse(features.subset.2$core == 1,"Core","NonCore")) #Set core\noncore attributes

#Even if I resample the matrix it dosnot work.
#adj_matrix.sub = adj_matrix[features.subset.2$sample,features.subset.2$sample]
#sub.g1 <- graph.adjacency(adj_matrix.sub, weighted = T, mode = "directed")

# 1.3 get the directed features of all the nodes:
Indegree <- degree(sub.g1, mode= "in")
Outdegree <- degree(sub.g1, mode = "out")

Instrength <- strength(sub.g1, mode = "in")
Outstrength <- strength(sub.g1, mode = "out")

```

![image](https://global.discourse-cdn.com/free1/uploads/igraph/original/1X/6a697dcf82468a0b48510a1ac4ac7effa036363a.png)

---

<div class="post-metadata">

**Author:** ![vtraag](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/vtraag/32/38_2.png) [@vtraag](https://igraph.discourse.group/u/vtraag)\
**Post date:** [24 May 2022 08:54 UTC](https://igraph.discourse.group/t/strength-returns-nas-when-used-in-for-loop/1228/2 "2022-05-24T08:54:56Z")

</div>

Sorry, I’m unable to replicate the problem. Doing something like the followings works exactly as expected:

```nohighlight
library(igraph)
G <- graph.famous('Zachary')
E(G)$weight = 0.5
print(strength(G, mode='out'))
G1 <- induced_subgraph(G, vids=c(1:10) )
print(strength(G1, mode='out'))

```

Could you perhaps try to make a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example), so that we can replicate the problem and perhaps suggest a solution?

---

<div class="post-metadata">

**Author:** ![idado6](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/idado6/32/766_2.png) [@idado6](https://igraph.discourse.group/u/idado6)\
**Post date:** [24 May 2022 16:38 UTC](https://igraph.discourse.group/t/strength-returns-nas-when-used-in-for-loop/1228/3 "2022-05-24T16:38:02Z")

</div>

Hey I appreciate the help!  
I was able to resolve the problem. For some reason (I guess a mistake) when I read the adjacency matrix to a graph object I deleted the diag = F. And since my diagonal is contacting onlt NA, it cant sum the woghts.  
Thanks!
