Do NOT use 1 length x in for…

Do NOT use 1:length(x) in for statement in R.
Because when x is empty, the loop will run over 1:0, which is twice.

> y <- as.numeric()
> length(y)
[1] 0
> for (i in 1:length(y)) { print("y")}
[1] "y"
[1] "y"
> for (i in 1:seq(along=y)) { print("y")}
Error in 1:seq(along = y) : argument of length 0

By microbe

Like to cook for microorganisms and human. Play football, tennis, and baseball.

Leave a Reply

Your email address will not be published. Required fields are marked *