[R] Subarrays

Peter Dalgaard p.dalgaard at biostat.ku.dk
Fri Apr 29 19:44:39 CEST 2005


Tony Plate <tplate at acm.org> writes:

> Question for language experts: is this the best way to create and
> manipulate R language expressions that contain empty arguments, or are
> there other preferred ways?

It's generally a pain anyways... One possible alternative is to use
TRUE instead

> ix <-rep(list(T),4)
> ix[c(1,4)] <- c(3,2)
> do.call("[",c(list(v),ix))
     [,1] [,2] [,3] [,4]
[1,]   67   83   99  115
[2,]   71   87  103  119
[3,]   75   91  107  123
[4,]   79   95  111  127

The alist() function can be used to generate the missing argument in a
somewhat clean way. E.g.,

ix <- as.list(dim(v))
ix[c(1,4)] <- c(3,2)
ix[-c(1,4)] <- alist(a=) # name disappears
do.call("[",c(list(v),ix))

(BTW, it surprised me a little that you don't need as.list() on the
right hand side of ix[c(1,4)] <- c(3,2), anyone know the rationale?)


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list