Skip to content
Prev 59475 / 63430 Next

Unexpected behavior of '[' in an apply instruction

Le 12/02/2021 ? 22:23, Rui Barradas a ?crit?:
There is another approach -- produce a call to `[`() putting there 
"required number of commas in their proper places" programmatically. 
Even if it does not lead to a very readable expression, I think it 
merits to be mentioned.

 ? x <- array(1:60, dim = c(10, 2, 3))
 ? ld=length(dim(x))
 ? i=1 # i.e. the first row but can be a slice 1:5, whatever
 ? do.call(`[`, c(alist(x, i), alist(,)[rep(1,ld-1)], alist(drop=FALSE)))

Best,
Serguei.