Ghoostfire
Ghoostfire

Reputation: 21

Error : declared object not Found in RStudio code

I am new to R, and I'm working on it for a college project, I try to color the nodes of a node network based on another list called "cores" using the function setColor, but when I try to run it, it results in the error "Error in setColor(i): object "cores" not found", resulting in a plot that is not colored.

It's important to note, it does run SOMETIMES, even though I don't change ANYTHING in the code

Here's the code that I have written:

library(igraph)

g1<- graph(edges=c(1, 131, 1, 123, 1, 114, 2, 67, 2, 65, 3, 47, 3, 1, 3, 17, 4, 120, 4, 181, 4, 81, 5, 153, 5, 33, 5, 21, 5, 181, 6, 126, 6, 89, 7, 78, 7, 159, 8, 178, 8, 147, 8, 107, 8, 79, 9, 35, 9, 80, 10, 187, 10, 192, 11, 75, 11, 70, 11, 86, 11, 48, 12, 22, 12, 39, 12, 190, 12, 140, 13, 175, 13, 191, 14, 169, 14, 185, 14, 54, 15, 166, 15, 84, 15, 59, 15, 88, 16, 50, 16, 47, 17, 92, 17, 165, 18, 161, 18, 177, 18, 94, 19, 63, 19, 171, 19, 144, 19, 166, 20, 24, 20, 187, 21, 30, 21, 166, 22, 13, 22, 25, 22, 45, 23, 112, 23, 195, 24, 34, 24, 99, 24, 114, 24, 43, 25, 80, 25, 7, 25, 134, 26, 120, 26, 92, 26, 105, 27, 13, 27, 64, 27, 48, 28, 191, 28, 185, 28, 34, 28, 95, 29, 27, 29, 113, 29, 92, 30, 191, 30, 185, 30, 50, 30, 94, 30, 24, 31, 116, 31, 187, 32, 161, 32, 3, 33, 173, 33, 30, 34, 122, 34, 171, 34, 64, 35, 55, 35, 150, 36, 132, 36, 50, 36, 85, 36, 155, 37, 43, 37, 173, 37, 171, 38, 66, 38, 132, 39, 103, 39, 175, 39, 196, 40, 44, 40, 133, 41, 117, 41, 197, 41, 131, 42, 20, 42, 4, 42, 48, 43, 50, 43, 21, 43, 19, 44, 164, 44, 19, 45, 172, 45, 118, 46, 118, 46, 76, 46, 90, 47, 92, 47, 184, 47, 136, 48, 36, 48, 101, 48, 46, 49, 141, 49, 158, 49, 50, 50, 18, 50, 15, 50, 183, 50, 161, 51, 121, 51, 140, 51, 191, 52, 118, 52, 69, 52, 56, 53, 32, 53, 191, 54, 152, 54, 61, 55, 7, 55, 101, 56, 112, 56, 32, 57, 121, 57, 137, 58, 70, 58, 56, 58, 53, 59, 190, 59, 43, 59, 11, 60, 191, 61, 9, 61, 33, 61, 72, 62, 56, 62, 173, 63, 170, 63, 185, 63, 34, 63, 66, 64, 59, 64, 97, 65, 24, 65, 27, 65, 55, 65, 50, 65, 63, 66, 44, 66, 99, 67, 140, 67, 123, 67, 130, 68, 135, 68, 188, 68, 15, 69, 21, 69, 179, 69, 162, 70, 162, 70, 193, 70, 119, 71, 111, 71, 149, 71, 131, 71, 109, 72, 36, 72, 184, 72, 176, 73, 19, 73, 182, 73, 75, 73, 172, 74, 35, 74, 54, 74, 134, 74, 97, 75, 110, 75, 16, 75, 7, 76, 190, 76, 146, 76, 144, 77, 71, 77, 68, 77, 129, 77, 74, 78, 44, 78, 57, 79, 36, 79, 152, 79, 191, 80, 24, 80, 72, 81, 72, 81, 140, 81, 186, 82, 75, 82, 95, 82, 145, 82, 14, 83, 78, 83, 68, 83, 81, 84, 158, 84, 49, 84, 157, 84, 100, 84, 135, 85, 81, 85, 124, 85, 110, 86, 164, 86, 155, 86, 172, 87, 47, 87, 175, 87, 40, 87, 81, 88, 2, 88, 36, 88, 191, 88, 128, 89, 140, 89, 66, 89, 191, 89, 51, 90, 101, 90, 162, 90, 113, 91, 147, 91, 22, 91, 15, 92, 70, 92, 192, 92, 112, 92, 79, 93, 63, 93, 113, 93, 20, 94, 172, 94, 173, 94, 109, 94, 176, 95, 111, 95, 43, 95, 28, 96, 68, 96, 100, 97, 26, 97, 165, 97, 147, 98, 194, 98, 94, 99, 110, 99, 23, 99, 150, 100, 127, 100, 15, 100, 166, 100, 197, 101, 57, 101, 137, 102, 23, 102, 133, 103, 102, 103, 95, 103, 94, 104, 163, 104, 60, 105, 168, 105, 112, 106, 127, 106, 61, 107, 30, 107, 63, 107, 83, 108, 151, 108, 30, 109, 24, 109, 164, 110, 183, 110, 1, 110, 137, 110, 148, 111, 1, 111, 28, 111, 121, 111, 160, 112, 170, 112, 162, 113, 151, 113, 48, 113, 146, 114, 22, 114, 115, 114, 36, 115, 87, 115, 43, 116, 10, 116, 147, 116, 133, 117, 105, 117, 41, 117, 57, 117, 54, 118, 20, 118, 133, 118, 64, 119, 47, 119, 139, 119, 198, 120, 98, 120, 191, 121, 3, 121, 15, 121, 57, 121, 1, 121, 74, 122, 133, 122, 74, 122, 174, 123, 134, 123, 192, 124, 97, 124, 18, 124, 64, 124, 181, 124, 40, 125, 115, 125, 27, 125, 196, 125, 109, 126, 20, 126, 77, 126, 31, 126, 78, 127, 160, 127, 80, 127, 95, 127, 180, 128, 95, 128, 58, 128, 113, 129, 31, 129, 164, 130, 53, 130, 57, 131, 143, 131, 8, 131, 58, 131, 36, 132, 13, 132, 109, 132, 136, 132, 173, 133, 198, 133, 112, 134, 143, 134, 40, 135, 189, 135, 39, 135, 94, 135, 148, 136, 21, 136, 143, 137, 89, 137, 79, 137, 115, 137, 171, 138, 136, 138, 97, 139, 120, 139, 179, 139, 153, 140, 116, 140, 22, 140, 162, 140, 133, 141, 164, 141, 8, 141, 38, 141, 32, 141, 57, 142, 190, 142, 183, 143, 199, 143, 84, 144, 103, 144, 33, 144, 166, 144, 17, 145, 41, 145, 105, 145, 142, 146, 1, 146, 110, 147, 92, 147, 88, 147, 75, 147, 174, 148, 141, 148, 171, 149, 72, 149, 161, 149, 116, 150, 34, 150, 156, 151, 156, 151, 57, 152, 177, 152, 150, 153, 11, 153, 76, 154, 182, 154, 2, 154, 62, 155, 91, 155, 31, 155, 159, 156, 90, 156, 195, 156, 12, 156, 139, 157, 169, 157, 80, 157, 79, 158, 74, 158, 11, 158, 178, 159, 7, 159, 129, 160, 75, 160, 58, 160, 189, 160, 66, 161, 66, 161, 160, 161, 96, 161, 74, 162, 150, 162, 43, 162, 127, 162, 46, 163, 55, 163, 17, 163, 6, 164, 165, 164, 123, 165, 140, 165, 139, 165, 19, 165, 62, 166, 128, 166, 185, 166, 179, 166, 129, 166, 32, 167, 102, 167, 82, 168, 37, 168, 159, 169, 145, 169, 177, 170, 51, 170, 48, 170, 76, 171, 153, 171, 86, 172, 116, 172, 135, 172, 101, 173, 195, 173, 156, 174, 10, 174, 47, 174, 68, 174, 165, 175, 84, 175, 153, 175, 75, 175, 114, 176, 151, 176, 71, 177, 40, 177, 187, 177, 68, 178, 146, 178, 73, 178, 11, 179, 137, 179, 34, 179, 59, 179, 14, 180, 38, 180, 191, 181, 98, 181, 149, 181, 195, 182, 8, 182, 141, 183, 53, 183, 80, 183, 7, 184, 6, 184, 38, 185, 23, 185, 105, 186, 119, 186, 138, 187, 111, 187, 163, 188, 148, 188, 7, 188, 123, 189, 9, 189, 75, 189, 138, 189, 64, 190, 179, 190, 129, 190, 80, 190, 75, 191, 179, 191, 101, 191, 43, 191, 181, 192, 119, 192, 18, 193, 149, 193, 119, 194, 170, 194, 77, 194, 13, 194, 22, 195, 35, 195, 144, 195, 65, 196, 3, 196, 164, 196, 162, 197, 28, 197, 134, 197, 100, 197, 173, 198, 86, 198, 111, 198, 8, 199, 114, 199, 183, 199, 23, 199, 11, 200, 131, 200, 28, 200, 12, 200, 173), directed=F )
  cores<- c(1, 3, 3, 3, 3, 1, 3, 3, 3, 1, 3, 3, 3, 3, 3, 3, 1, 3, 3, 3, 3, 3, 3, 1, 3, 1, 3, 3, 3, 1, 3, 1, 3, 1, 3, 3, 3, 1, 3, 3, 3, 3, 1, 3, 3, 1, 3, 3, 3, 3, 3, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 3, 3, 3, 3, 3, 1, 3, 1, 3, 1, 3, 3, 3, 3, 1, 3, 3, 3, 3, 3, 1, 3, 1, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 3, 3, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 3, 3, 3, 3, 1, 3, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 3, 3, 3, 1, 3, 3, 3, 3, 3, 3, 3, 1, 3, 1, 3, 3, 3, 1, 3
             )
  
  V(g1)
  setColor<-function(n)
  {
    if(cores[n] == 1)
    {
      return("green")}
    else if (cores[n] == 2){
      return("blue")
    } 
    else if (cores[n] == 3){
      return("orange")
    } 
    else if (cores[n] == 4){
      return("red")
    } 
    else if (cores[n] == 5){
      return("grey")
    } 
    else if (cores[n] == 6){
      return("black")
    } 
  }
  for(i in 1:200){
    V(g1)[i]$color <- setColor(i)
  }
plot(g1,vertex.size = 5, vertex.label=NA)
legend(x= "bottomleft",legend=c("Saudável com máscara", "Saudavel sem mascara", "Infectado com mascara", "Infectado sem mascara"), fill = c("green" ,"blue","orange","red"), pch=c(".",".", ".", "."),cex = 0.7)
  

Upvotes: 1

Views: 256

Answers (1)

ThomasIsCoding
ThomasIsCoding

Reputation: 101818

You should use V(g1)$color[i] rather than V(g1)[i]$color, i.e.,

for (i in 1:200) {
  V(g1)$color[i] <- setColor(i)
}

and you will get

enter image description here


A more efficient way to set colors is

V(g1)$color <- c("green", "blue", "orange", "red", "grep", "black")[cores]

Upvotes: 0

Related Questions