0

What am I missing here? I already cleaned the workspace and restartet the session. Here is my the part of the code that seems to cause trouble. I am trying to average an bayesian networks arc strenghts from a strength object, then save all nodes of that averaged graph and then calculate the relevant nodes, saved as vector of character strings:

averaged = averaged.network (strength)
nodes.averaged = unique(unlist(arcs(averaged)))
relevant.nodes = nodes(averaged)[sapply(nodes.averaged, degree, object = averaged) > 0]
averagedNew = subgraph(averaged,relevant.nodes)

And then I get this Error:

Error in check.nodes(nodes, graph = x, max.nodes = length(x$nodes)) : 
  nodes must be a vector of character strings, the labels of the nodes.

This is my relevant.nodes:

[1] "V81"  "V97"  "V114" "V55"  "V93"  "V119" "V102" "V23"  "V24"  "V76"  "V67"  "V29"  "V33"  "V84"  "V89"  "V73" 
[17] "V82"  "V71"  "V45"  "V7"   "V21"  "V109" "V5"   "V41"  "V65"  "V118" "V6"   "V20"  "V100" "V15"  "V59"  "V94" 
[33] "V57"  NA     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA    
[49] NA     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA    
[65] NA     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA    
[81] NA     NA     NA     NA     NA     NA     NA     NA     NA     NA

When I checked the source code for the place, where this error message is thrown, I found the if-statement (Link)

  # nodes must be a vector of character strings.
  if (!is(nodes, "character"))
    stop("nodes must be a vector of character strings, the labels of the nodes.")

and ran it:

(!is(relevant.nodes, "character"))

and the result is

[1] FALSE

Does anybody has an idea? I am rather new to R and still found myself to learn the difference between = and <- just yesterday, so please don't be too harsh on me.

user20650
  • 24,654
  • 5
  • 56
  • 91
Phise
  • 11
  • 2
  • I think the issue could be the line `nodes.averaged = ...` . You use `unlist` which does do anything as `averaged` is a matrix. Try using `nodes.averaged = unique(c(arcs(averaged)))`, or `as.vector` instead oc `c`, or you can directly get the nodes using `nodes(averaged)`. If this doesnt work can you add a reproducible example, say using one of the package datasets – user20650 Aug 15 '17 at 21:43
  • That is even better than my solution, because it doesn't create the problem with NA data in the vector in the first place. In my case it worked, so to use `nodes(averaged)` instead of `unique(c(arcs(averaged)))`, but there was supposed to be a Datastructure storaging multiple networks (for `average` at `unique(c(arcs(averaged)))`. Then one would have to use `c` etc. – Phise Aug 15 '17 at 22:30

1 Answers1

1

I found my mistake... The vector of character strings is not allowed to have any empty data. This has done the trick:

relevant.nodes = relevant.nodes[!is.na(relevant.nodes)]
Phise
  • 11
  • 2
  • why would some of your nodes be NA? – user20650 Aug 15 '17 at 21:45
  • They shouldn't and they aren't. I just had two diffent looking data types and when they "merged" at `relevant.nodes = nodes(averaged)[sapply(nodes, degree, object = averaged) > 0]` it screwed up the vector somehow. – Phise Aug 15 '17 at 22:33