R: What is the easiest way (single line?) To create an expression vector?

Is there a way to create a vector of expressions on a single line? I only know two-line with an ugly for-loop:

vexpr <- vector("expression", 7) for(j in 1:7) vexpr[j] <- substitute(expression(italic(X[j.])), list(j.=j))[2] 
+6
source share
1 answer
 as.expression( sapply(1:7, function(x) bquote(italic(X[.(x)]))) ) #----------- # expression(italic(X[1L]), italic(X[2L]), italic(X[3L]), italic(X[4L]), # italic(X[5L]), italic(X[6L]), italic(X[7L])) identical(vexpr, as.expression( sapply(1:7, function(x) bquote(italic(X[.(x)]))) ) ) #[1] TRUE 

also:

 parse(text= paste("italic(X[", 1:7, "])", sep="") ) # fewer keystrokes #-------- # expression(italic(X[1]), italic(X[2]), italic(X[3]), italic(X[4]), # italic(X[5]), italic(X[6]), italic(X[7])) 

(The second will not pass the identical () test, because it carries the legacy of building it with it. I think these are side effects in byte code, improved in R version 2.14.0, so it may look different in more in earlier versions, you can verify this by applying str () to it. However, it passes the test to apply the appropriate x-axis labels to plot(1:7, xaxt="n"); axis(1,at=1:7, labels=...) )

+8
source

Source: https://habr.com/ru/post/902456/


All Articles