Search code examples
rgraphsavenested-loops

Save graphs and statistics from nested loops


I came across the question on saving graph plots and statistics from nested loops in R. What I've got is an unbalanced panel data of social networks. In below example it has the network connection in 3 locations from 2001 to 2003. I aimed to generate network graphs along with some statistics in each year of each location (therefore nine graphs and nine SNA statistics from the example). But I got confused on setting results container beforehand, especially when the loop is nested. I would much appreciate it if anyone can help with it.

> # import file 
> el <- read.csv(file.choose(), header = TRUE )  
> print(el)                                   # example data
   bname sname year loc
1      a     b 2001   1
2      a     c 2001   1
3      a     b 2002   1
4      b     c 2002   1
5      b     e 2002   1
6      e     a 2003   1
7      b     a 2003   1
8      a     c 2003   1
9      b     e 2001   2
10     a     c 2002   2
11     a     b 2002   2
12     b     c 2002   2
13     c     e 2002   2
14     c     e 2003   2
15     b     e 2003   2
16     a     b 2003   2
17     a     b 2001   3
18     a     c 2001   3
19     a     e 2001   3
20     a     d 2001   3
21     a     b 2002   3
22     b     c 2002   3
23     b     c 2003   3
> # Import objects
> loc <- as.vector(unique(el$loc))
> year <- as.vector(unique(el$year))
> start <- 2001
> end <- 2003

> # Result container
> mylist <- vector('list', length(loc))       # final list
> acc <- paste("")                            # vector for avg. cluster coef.
> sna <- cbind(year, acc)                     # matrix for each location

> # Loop, i for location, acc. as example
> for (i in unique(loc)) {
+   
+   for (j in start:end) {
+     el_s <- subset(el, loc==i & year==j)    # pick subsample                  
+     edge <- el[order(el_s$bname, el_s$sname),]
+     nw <- graph_from_data_frame(d= el_s, directed = T)
+     
+     # graph plotting 
+     png("network"i""j".jpg")                        
 Error: unexpected symbol in:
 "    # graph plotting 
    png("network"i"                           # Error here -- Can the name be save in "network_i_j" mode?
>     plot(nw)
>     dev.off()
null device 
          1 
+     
+     # SNA statistics
+     sna[j,"acc"] <- transitivity(nw, type = "average") # Error here
+     
+   }  
+   
+   mylist[[i]] <- sna
+ }
Error in `[<-`(`*tmp*`, j, "acc", value = transitivity(nw, type = "average")) : 
  subscript out of bounds

I was trying to adopt the methods in previous post for douple-loops, and I think I get confused in terms of saving the results in the inner loop. Also, when saving the graph, I don't know if R can save the name in "network_location_year" mode? I did that in the way Stata did so I guess it probably doesn't apply to R.

Thanks again in advanced!!


Solution

  • it's an issue with formatting file names. When opening the png device, you should use

    png(sprintf("network_%s_%s.png", i, j))
    

    instead.

    And for this line,

    sna[j,"acc"] <- transitivity(nw, type = "average")
    

    you get subscript out of bounds because j is between 2001 and 2003, rather than 1 to 3. you'll want

    sna[sna$year == j,"acc"] <- transitivity(nw, type = "average")
    

    instead.

    BTW if you want jpg rather than png, you can use

    jpeg(sprintf("network_%s_%s.jpg", i, j))