Brandon Bertelsen
Brandon Bertelsen

Reputation: 44648

Error Messages in R after a for loop

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?

Upvotes: 2

Views: 6557

Answers (1)

Shane
Shane

Reputation: 100164

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

Upvotes: 5

Related Questions