[R] expression with running index
Prof Brian Ripley
ripley at stats.ox.ac.uk
Sun Apr 25 20:40:44 CEST 2004
lapply(-1:2, function(i) substitute(expression(b[i]), list(i=i)))
would be a good start. (Note that what it gives is
which is not what you asked for but is what I think you intended.
Then we can elaborate this to
f <- function(ind, vec)
function(i, vec) substitute(expression(vec[i]),
f(-1:2, "b") and f(c("f", "g", "h"), "b") both work
Also, I am not sure you need the expression() in there, as without it you
have a language call which will almost certainly do.
On Sun, 25 Apr 2004, Tamas Papp wrote:
> I need a list of expression of the form expression(b[i]), where i is a
> running index. So for example, if i <- -1:2, I would like to have a
> list equivalent to
> list(expression(b[-1]), expression(b), expression(b), expression(b))
> "i" might be a character vector (like c("f", "g", "h"))
> Could somebody help me out by writing a function that produces the
> list above for a given string in place of "b" and a vector of subscripts?
> Sorry if this has been discussed before, I tried searching the
> archives but "expression" as a keyword gives too many results on
> different subjects.
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help