[R] Changing selected elements of an array

Joshua Wiley jwiley.psych at gmail.com
Mon May 21 18:31:11 CEST 2012


On Mon, May 21, 2012 at 9:27 AM, Øystein Godøy <o.godoy at met.no> wrote:
> 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.

I cannot reproduce this behavior.  On my system, the output has 100 elements.


> 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/



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/



More information about the R-help mailing list