Skip to content
Prev 131664 / 398502 Next

seq_len

Charilaos Skiadas wrote:
The essence of this feature is to get the correct index sequences when 
performing matrix lookups and loops without causing errors in the 
matrices or subscripts. If I build the correct x=seq_len or 
x=seq(along=) and use the 'in x' not 'in 1:dim(x)[1]' or 'in 
1:length(x)', my code will execute correctly without the NA/NAN error or 
subscript out of bounds, etc.  Is this correct?

Examples
 > x=numeric()
 > x
numeric(0)
 > for ( i in 1:x) print(i)
Error in 1:x : NA/NaN argument
 > for ( i in x) print(i)
 > x=2
 > for ( i in x) print(i)
[1] 2
 > seq_len(x)
[1] 1 2
 > x=seq_len(x)
 > for ( i in x) print(i)
[1] 1
[1] 2
 > for ( i in 1:x) print(i)
[1] 1
Warning message:
In 1:x : numerical expression has 2 elements: only the first used
 > for ( i in x) print(i)
[1] 1
[1] 2
 > x=2
 > for ( i in 1:x) print(i)
[1] 1
[1] 2
 > for ( i in x) print(i)
[1] 2
 > for (i in 1:length(x)) print(i)
[1] 1
 >


Thank you
Joe