Alexy, note that negative indices have a different meaning in R, namely they indicate elements to remove. v[-(1:i)] would mean all but the elements from 1 to i (make sure i is non-negative first). So if you want:
All elements up to i: v[1:i] All elements from i: v[-1:(i-1)] You can actually wrap these in functions likely. If you know *how many* elements from the end you want, you can use "tail": x<-1:10 tail(x,1) # => 10 Haris Skiadas Department of Mathematics and Computer Science Hanover College On Nov 24, 2007, at 7:28 AM, Gabor Csardi wrote: > Please someone correct me if i'm wrong, but i think this is impossible > with the current R language. ':' is an ordinary function (see > get(":")) just like "[", so v[1:3] is actually the composition of two > functions, it is the same as "["(":"(1,3)). The ":" has no idea about > whether it'll be embedded into a "[" function of not, it certainly > cannot know the length of the vector i'll be indexed with. > > While this has the drawback you mentioned, it also has some > advantages, when someone writes something like > > lapply(l, "[[", 1) > > or > > lapply(10:1, ":", 20) > > So, I'm afraid you'll have to live with length(v). > > Gabor > > On Sat, Nov 24, 2007 at 12:49:59PM +0300, Alexy Khrabrov wrote: >> What are idioms for taking a head or a tail of a vector, either up to >> an index, or from an index to the end? Also -- is it necessary to >> use length(v) to refer to the last element? E.g., Python has >> >> v[:3] # indices 0,1,2 >> v[3:] # indices 3,4,... >> v[-1] # the last element of v >> v[:-1] # all but last >> >> Cheers, >> Alexy >> >> ______________________________________________ >> R-help@r-project.org mailing list >> 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. > > -- > Csardi Gabor <[EMAIL PROTECTED]> MTA RMKI, ELTE TTK > ______________________________________________ R-help@r-project.org mailing list 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.