tags:

views:

134

answers:

1

This is a bit of a shot in the dark, but I have a script that does exactly what I expect it to do, yet, at the very end of the script I get an error like this:

Error in `[<-.data.frame`(`*tmp*`, "label", value = c(1L, 0L)) : 
  replacement has 2 rows, data has 0

In terms of an answer, I'm looking for general suggestions on how to track errors like this in R, best practices for using loops and double checking that they "made it through".

Any thoughts, suggestions, or past experiences that could relegate or inform an error message like this?

+5  A: 

I already included my comments about debugging practices in this related question. But regarding the specific message that you show here: that means that you're trying to write out 2 rows to some dataset that has 0 rows. Something like this:

x <- data.frame(y=NULL)
x$y <- 1:2
Shane
traceback() works like a charm, I found the error. Cheers for that.
Brandon Bertelsen
Great. FYI: I don't think that traceback works properly with everything (e.g. S4 or lapply).
Shane
Thank you, I'll definitely keep that in mind.
Brandon Bertelsen