[R] Changing selected elements of an array

Øystein Godøy o.godoy at met.no
Mon May 21 18:27:58 CEST 2012


Hi Joshua,

Many thanks for your quick reply. 

> You can do it by passing a matrix for indexing instead of two vectors.
>  Here's an example:
> 
> tmpmat <- matrix(NA, nrow = 10, ncol = 10,
>   dimnames = list(letters[1:10], LETTERS[1:10]))
> 
> tmpmat[cbind(c("d", "e", "f"), c("D", "E", "F"))] <- 100
> tmpmat
> 
> The matrix is created using cbind() to columnwise bind the two vectors
> together.  Then it does what you want I think.
> 
> Hope this helps,

This looks interesting and is what I want, but I am not fully understanding 
the output I receive. The input array has 100 elements while the resulting 
vector after replacement is 106 elements long. I have tried to understand the 
manual on this, but it is yet not obvious for me how I am supposed to handle 
this output.

All the best
Øystein
-- 
Dr. Oystein Godoy
Norwegian Meteorological Institute 
P.O.BOX 43, Blindern, N-0313 OSLO, Norway
Ph: (+47) 2296 3000 (switchb) 2296 3334 (direct line)
Fax:(+47) 2296 3050 Institute home page: http://met.no/



More information about the R-help mailing list