Chuck
Chuck

Reputation: 3852

Igraph Invalid Indexing Error when using Ifelse

I have two vectors of nodes:

bad_node_pair
+ 2/2 vertices, named:
[1] 1949 1967

remaining_nodes
+ 5/? vertices, named:
[1] 1947 1948 1949 1967 1968

I test whether the bad_node_pair exists in the remaining_nodes, and if so, return the ones that do appear:

bad_node_pair[names(bad_node_pair) %in% names(remaining_nodes)]
+ 2/2 vertices, named:
[1] 1949 1967

However, when I put this in a loop, I get:

ifelse(
  bad_node_pair[names(bad_node_pair) %in% names(remaining_nodes)], 
       print(1), 
       print(0)
       )
[1] 1
Error in `[<-.igraph.vs`(`*tmp*`, test & ok, value = c(1, 1)) : 
  invalid indexing

It prints the answer, but throws that error.

What is going on?


Data for bad nodes:

df1 <- read.table(header=T, text=" from   to
8 1949 1967")
bad_g <- graph.data.frame(df1, directed=FALSE)
bad_node_pair <- V(bad_g)

Data for good nodes:

df2 <- read.table(header=T, text=" from   to
1 1947 1948
2 1947 1949
3 1947 1967
4 1947 1968
5 1948 1949
6 1948 1967
7 1948 1968
8 1949 1968")
g <- graph.data.frame(df2, directed=FALSE)
remaining_nodes <- V(g)

Upvotes: 0

Views: 518

Answers (1)

MRau
MRau

Reputation: 336

If you want to use ifelse, you have to pass a logical argument:

ifelse(any(bad_node_pair[names(bad_node_pair) %in% names(remaining_nodes)]), 1, 0)
[1] 1

Or:

ifelse(names(bad_node_pair) %in% names(remaining_nodes), 1, 0)
[1] 1 1

Depending on what you want to obtain (I am not sure if I understand your question correctly)

Upvotes: 1

Related Questions