Hi WizaRds, In MATLAB you can do x=1:10 and then specify x(2:end) to get 2 3 4 5 6 7 8 9 10 or whatever (note that in MATLAB the parenthetic index notation is used, not brackets as in R). The point is that 'end' allows you to refer to the final index point of the array. Obviously there isn't much gain in syntax when the variable name is x, but when it's something like hereIsABigVariableName(j:end-i) it makes things a lot more readable than hereIsABigVariableName(j:length(hereIsABigVariableName)-i) In R I could do: n<- length(hereIsABigVariableName) hereIsABigVariableName[j:n-i] but I'd like to use something like 'end', if it exists. Am I missing something obvious in R that does what 'end' does in MATLAB? Thanks, Jack. --------------------------------- [[alternative HTML version deleted]]
Jack, in R ":" is an ordinary function with two arguments, ie. 1:10 is a function call with arguments 1 and 10. This way at the time ":" is evaluated the variable which will be indexed is not known; it is thus impossible to know its length. This is why it is not easy to implement "end" in R. Since R is very good at computing 'on the language' there might be a way (i would call it hack) to implement 'end' but i'm not aware of it. Eg. you might consider writing a function like idx <- function(x, i) { i <- substitute(i) i <- eval(i, c(as.list(parent.frame()), end=length(x))) x[i] } and then you can write things like this: x <- 1:10 idx(x, 2:end) idx(x, 2:(end-1)) R gurus, please correct me if i'm wrong. Gabor On Wed, Aug 09, 2006 at 05:30:47PM -0700, John McHenry wrote:> Hi WizaRds, > > In MATLAB you can do > > x=1:10 > > and then specify > > x(2:end) > > to get > > 2 3 4 5 6 7 8 9 10 > > or whatever (note that in MATLAB the parenthetic index notation is used, not brackets as in R). The point is that 'end' allows you to refer to the final index point of the array. > > Obviously there isn't much gain in syntax when the variable name is x, but when it's something like > > hereIsABigVariableName(j:end-i) > > it makes things a lot more readable than > > hereIsABigVariableName(j:length(hereIsABigVariableName)-i) > > In R I could do: > > n<- length(hereIsABigVariableName) > hereIsABigVariableName[j:n-i] > > but I'd like to use something like 'end', if it exists. > > Am I missing something obvious in R that does what 'end' does in MATLAB? > > Thanks, > > Jack. > > > --------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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 <csardi at rmki.kfki.hu> MTA RMKI, ELTE TTK
Hi On 9 Aug 2006 at 17:30, John McHenry wrote: Date sent: Wed, 9 Aug 2006 17:30:47 -0700 (PDT) From: John McHenry <john_d_mchenry at yahoo.com> To: r-help at stat.math.ethz.ch Subject: [R] Is there a better way than x[1:length(x)-1] ?> Hi WizaRds, > > In MATLAB you can do > > x=1:10 > > and then specify > > x(2:end) > > to get > > 2 3 4 5 6 7 8 9 10 > > or whatever (note that in MATLAB the parenthetic index notation is > used, not brackets as in R). The point is that 'end' allows you to > refer to the final index point of the array. > > Obviously there isn't much gain in syntax when the variable name is x, > but when it's something like > > hereIsABigVariableName(j:end-i) > > it makes things a lot more readable than > > hereIsABigVariableName(j:length(hereIsABigVariableName)-i) > > In R I could do: > > n<- length(hereIsABigVariableName) > hereIsABigVariableName[j:n-i] > > but I'd like to use something like 'end', if it exists.It probably does not exist (but in R you never know :-) and you can easily to construct it yourself x <- 1:10 end <- function(x) length(x) x[2:end(x)] [1] 2 3 4 5 6 7 8 9 10 > HTH Petr> > Am I missing something obvious in R that does what 'end' does in > MATLAB? > > Thanks, > > Jack. > > > --------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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.Petr Pikal petr.pikal at precheza.cz
Gabor Grothendieck
2006-Aug-10 12:38 UTC
[R] Is there a better way than x[1:length(x)-1] ?
On 8/9/06, John McHenry <john_d_mchenry at yahoo.com> wrote:> Hi WizaRds, > > In MATLAB you can do > > x=1:10 > > and then specify > > x(2:end) > > to get > > 2 3 4 5 6 7 8 9 10 >In R you could do the above via: x[-1]> or whatever (note that in MATLAB the parenthetic index notation is used, not brackets as in R). The point is that 'end' allows you to refer to the final index point of the array. > > Obviously there isn't much gain in syntax when the variable name is x, but when it's something like > > hereIsABigVariableName(j:end-i) > > it makes things a lot more readable than > > hereIsABigVariableName(j:length(hereIsABigVariableName)-i) > > In R I could do: > > n<- length(hereIsABigVariableName) > hereIsABigVariableName[j:n-i]In "R version 2.4.0 Under development (unstable) (2006-08-08 r38825)" available from CRAN, head and tail can have negative arguments: head(x, -2) is the same as x[1:8] using your x.> > but I'd like to use something like 'end', if it exists. > > Am I missing something obvious in R that does what 'end' does in MATLAB? > > Thanks, > > Jack. > > > --------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >