[R] Indexing With List Of Vectors (Replacement)

Gabor Grothendieck ggrothendieck at gmail.com
Fri Apr 7 01:24:43 CEST 2006


Try this:

do.call("[<-", c(list(b), idx, list(a)))

On 4/6/06, Paul Roebuck <roebuck at mdanderson.org> wrote:
> I have the following:
>
> > a <- matrix(1:10, nrow = 2, byrow = TRUE)
> > b <- array(as.integer(0), c(7, 5))
> > idx <- list()
> > length(idx) <- 2
> > dim(idx) <- c(1, 2)
> > idx[[1]] <- as.integer(1:2)
> > idx[[2]] <- as.integer(1:5)
>
> I can do the following, which works if 'b' is a matrix.
>
> > b[idx[[1]], idx[[2]]] <- a
> > b
>     [,1] [,2] [,3] [,4] [,5]
> [1,]    1    3    5    7    9
> [2,]    2    4    6    8   10
> [3,]    8    8    8    8    8
> [4,]    8    8    8    8    8
> [5,]    8    8    8    8    8
> [6,]    8    8    8    8    8
> [7,]    8    8    8    8    8
>
> Looking for a way to do this generically such that 'idx'
> with 'n' length can be used to index n-dimensional arrays.
>
> b[<<<translate 'idx' to array indices>>>] <- a
>
> ----------------------------------------------------------
> SIGSIG -- signature too long (core dumped)
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list