Hi All, I am trying to create a vector of expressions, where the elements in the expressions are contained in other vectors (i.e., they should be substituted). I made some attempts with substitute() and bquote(), but couldn't get this to work. My solution so far is: base <- 1:5 expo <- c(2,2,3,3,4) exvec <- as.expression(unname(mapply(function(x,y) bquote(.(x)^.(y)), base, expo))) plot(NA, NA, xlim=c(0,6), ylim=c(0,2)) text(1:5, 1, exvec) Any ideas how I could get this to work with substitute() and/or bquote()? Best, Wolfgang
Er, I'm confused. You post some code, the code does something. In which sense is this not what you want? This is slightly more direct:> mapply(function(x,y) as.expression(bquote(.(x)^.(y))), base, expo)expression(1L^2, 2L^2, 3L^3, 4L^3, 5L^4) but I sense that you are looking for something else? -pd> On 26 Mar 2019, at 14:27 , Viechtbauer, Wolfgang (SP) <wolfgang.viechtbauer at maastrichtuniversity.nl> wrote: > > Hi All, > > I am trying to create a vector of expressions, where the elements in the expressions are contained in other vectors (i.e., they should be substituted). I made some attempts with substitute() and bquote(), but couldn't get this to work. My solution so far is: > > base <- 1:5 > expo <- c(2,2,3,3,4) > exvec <- as.expression(unname(mapply(function(x,y) bquote(.(x)^.(y)), base, expo))) > > plot(NA, NA, xlim=c(0,6), ylim=c(0,2)) > text(1:5, 1, exvec) > > Any ideas how I could get this to work with substitute() and/or bquote()? > > Best, > Wolfgang > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.-- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
Apologies for not being clearer. The code does what I want, but I was wondering if there is a simpler way of doing this, using substitute()/bquote() directly without the mapply(). Best, Wolfgang -----Original Message----- From: peter dalgaard [mailto:pdalgd at gmail.com] Sent: Tuesday, 26 March, 2019 14:42 To: Viechtbauer, Wolfgang (SP) Cc: r-help mailing list Subject: Re: [R] Substitution in expressions Er, I'm confused. You post some code, the code does something. In which sense is this not what you want? This is slightly more direct:> mapply(function(x,y) as.expression(bquote(.(x)^.(y))), base, expo)expression(1L^2, 2L^2, 3L^3, 4L^3, 5L^4) but I sense that you are looking for something else? -pd> On 26 Mar 2019, at 14:27 , Viechtbauer, Wolfgang (SP) <wolfgang.viechtbauer at maastrichtuniversity.nl> wrote: > > Hi All, > > I am trying to create a vector of expressions, where the elements in the expressions are contained in other vectors (i.e., they should be substituted). I made some attempts with substitute() and bquote(), but couldn't get this to work. My solution so far is: > > base <- 1:5 > expo <- c(2,2,3,3,4) > exvec <- as.expression(unname(mapply(function(x,y) bquote(.(x)^.(y)), base, expo))) > > plot(NA, NA, xlim=c(0,6), ylim=c(0,2)) > text(1:5, 1, exvec) > > Any ideas how I could get this to work with substitute() and/or bquote()? > > Best, > Wolfgang
I believe you're going about this the wrong way. You seem to want mathematical expressions. Fot this, see ?plotmath. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Tue, Mar 26, 2019 at 6:28 AM Viechtbauer, Wolfgang (SP) < wolfgang.viechtbauer at maastrichtuniversity.nl> wrote:> Hi All, > > I am trying to create a vector of expressions, where the elements in the > expressions are contained in other vectors (i.e., they should be > substituted). I made some attempts with substitute() and bquote(), but > couldn't get this to work. My solution so far is: > > base <- 1:5 > expo <- c(2,2,3,3,4) > exvec <- as.expression(unname(mapply(function(x,y) bquote(.(x)^.(y)), > base, expo))) > > plot(NA, NA, xlim=c(0,6), ylim=c(0,2)) > text(1:5, 1, exvec) > > Any ideas how I could get this to work with substitute() and/or bquote()? > > Best, > Wolfgang > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]
Hi Bert, I am indeed creating a mathematical expression, but ?plotmath doesn't cover how to do such a vectorized substitution. Best, Wolfgang -----Original Message----- From: Bert Gunter [mailto:bgunter.4567 at gmail.com] Sent: Tuesday, 26 March, 2019 15:52 To: Viechtbauer, Wolfgang (SP) Cc: r-help mailing list Subject: Re: [R] Substitution in expressions I believe you're going about this the wrong way. You seem to want mathematical expressions. Fot this, see ?plotmath. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Tue, Mar 26, 2019 at 6:28 AM Viechtbauer, Wolfgang (SP) <wolfgang.viechtbauer at maastrichtuniversity.nl> wrote: Hi All, I am trying to create a vector of expressions, where the elements in the expressions are contained in other vectors (i.e., they should be substituted). I made some attempts with substitute() and bquote(), but couldn't get this to work. My solution so far is: base <- 1:5 expo <- c(2,2,3,3,4) exvec <- as.expression(unname(mapply(function(x,y) bquote(.(x)^.(y)), base, expo))) plot(NA, NA, xlim=c(0,6), ylim=c(0,2)) text(1:5, 1, exvec) Any ideas how I could get this to work with substitute() and/or bquote()? Best, Wolfgang