[R] Indexing With List Of Vectors (Replacement)
Paul Roebuck
roebuck at mdanderson.org
Thu Apr 6 20:52:05 CEST 2006
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)
More information about the R-help
mailing list