Hi, 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[0]), expression(b[1]), expression(b[2])) "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. Thanks, Tamas -- Tam??s K. Papp E-mail: tpapp at axelero.hu Please try to send only (latin-2) plain text, not HTML or other garbage.
Tamas Papp wrote:> Hi, > > 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[0]), expression(b[1]), expression(b[2])) > > "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. > > Thanks, > > Tamas >For example: lapply(as.double(-1:2), function(x) substitute(b[i], i = list(i=x))) Uwe Ligges
lapply(-1:2, function(i) substitute(expression(b[i]), list(i=i))) would be a good start. (Note that what it gives is [[1]] expression(b[as.integer(-1)]) which is not what you asked for but is what I think you intended. Then we can elaborate this to f <- function(ind, vec) lapply(ind, function(i, vec) substitute(expression(vec[i]), list(i=i, vec=vec)), vec=as.name(vec)) 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:> Hi, > > 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[0]), expression(b[1]), expression(b[2])) > > "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