On 26/08/2018 3:10 AM, Jeremie Juste wrote:
Duncan Murdoch <murdoch.dun...@gmail.com> writes:

for ( i in 1:length(var1)){

This is generally a bad idea:  if length(var1) == 0, it does the wrong
thing, since 1:0 is c(1L, 0L).  Better to use

for ( i in seq_along(var1) ) {



granted. One should check the validity of their variables before using
them but I argue that seq_along does not protect you from the
unexpected behaviour.

I don't see why you argue that. seq_along(var1) will give an empty vector if var1 is empty, so the loop won't run at all.


If the length of var1 should not be 0 so

stopifnot(length(var) > 0)
for ( i in 1:length(var1)){

     elem1 <-var1[i]
     elem2 <-  var2[i]

}

That's a possibility (I made your >0 correction), but maybe having length 0 isn't something that should trigger a fatal error, maybe it's just that no elements met some condition.

Duncan Murdoch

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to