Completely stumped about this error: Error in seq.default(start.at, NROW(data), by = by) : wrong sign in 'by' argument

空扰寡人 提交于 2021-01-29 02:23:45

问题


When I run this code, it works for about 100 iterations of the for loop then throws this error:Error in seq.default(start.at, NROW(data), by = by) : wrong sign in 'by' argument

Here is the data that I used, and here is my code...

library(igraph)
library(zoo)

#import network data as edgelist
fake.raw.data <- read.csv("fakedata.csv") 
fake.raw.data <- fake.raw.data[,2:3]
as.matrix(fake.raw.data)
#create igraph object from edglist data
fgraph <- graph_from_data_frame(fake.raw.data, directed = TRUE)

#finding the shortest paths  that go through "special chain"
POI <- list()
df.vertices <- get.data.frame(fgraph, what = "vertices")
list.vertices <- as.list(df.vertices[,1])
AverageEBForPath <- function(graph = fgraph, from, to, mode = "out", chain){
  browser()
  asp <- all_shortest_paths(graph, from = from, to = to, mode)$res



  for(i in seq_along(asp)){

    if(sum(rollapply(names(asp[[i]]), length(chain), identical, chain)) == 1){
      print(names(asp[[i]]))
    }

  }
}
AverageEBForPath(from = 32, to = V(fgraph), chain = c(32, 15, 9))

If anybody could help that would be extremely appreciated. I have been working on this for days, and I am really stuck.


回答1:


Looking through the code of rollapply, there's a bit where it works out where in the array to start the rolling. The code it uses is:

start.at <- if (partial < 0) 
    max(-min(width[[1]]), 0) + 1
else 1

Note that in the function itself, width is a list generated from the window width that you're trying to use and the alignment you want... Given that you're passing a window width of 3 and a default alignment of "centre", the width list the function has generated for the code above is a list of three integers: [-1, 0, 1]

Which means that, using the code above, it has decided that given you're after a centre aligned window of width 3, the place to start is the second value in the data (because max(-min(width[[1]]),0) + 1 in the above code evaluates to 2).

All very reasonable, but whilst all of the rest of the instances of asp[[i]] have either 2 or 3 vertices, asp[[100]] has only one vertex (as you rightly pointed out) - so it throws a bit of fit trying to find the second one in order to start rolling through it!

I'm not entirely sure what your function is eventually going to do, so the ball's a bit in your court to work out how best to handle this, I think you've got two options given what you're seeing:

Option 1

Use the partial = TRUE setting on your rollapply, which will just always start at the first vertex no matter what (see the code snippet above!)

Option 2

Use align="left" in your rollapply. In this case, the width list we saw in the rollapply function itself would be [0, 1, 2] for a window width of 3 and start.at would evaluate to 1.

Hope that rambling and convoluted attempt at an answer helps!



来源:https://stackoverflow.com/questions/35003910/completely-stumped-about-this-error-error-in-seq-defaultstart-at-nrowdata

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!