[R] mapping array

Joshua Wiley jwiley.psych at gmail.com
Sun Sep 12 02:25:55 CEST 2010


Hi Greg,

I am sketchy on a few details of C, but does something like this work
for you?  I just created C1 (renamed because C() is a function) with
two columns the first corresponding to A and the second to B.  Then I
just used the first column to select elements of A and the second to
select elements of B.  As a side note, this would work even if the
same element of B replaced multiple elements in A.

A <- array(NA, 100)
B <- array(sample(1:10, 25, replace = TRUE), 25)
C1 <- array(c(sample(1:100, 25, replace = FALSE),
              sample(1:25, 25, replace = FALSE)), c(25, 2))

C1 # look at C1

# Now use the 1st column to select elements of A
# and the 2nd to select elements of B to replace A
A[C1[,1]] <- B[C1[,2]]


Cheers,

Josh



On Sat, Sep 11, 2010 at 3:54 PM, Gregory Ryslik <rsaber at comcast.net> wrote:
>  Hi,
>
> Suppose I have array A with 100 elements all filled in with "N/A". Array B
> has 25 elements with actual values. Lastly, I have array C that provides a
> map of where to put the elements from array A into array B. Thus C would say
> put element 1 from B into element 3 from array A.
>
> I realize I can do this with a for loop. However, because I'm doing a random
> forest with this, I'd like to avoid a for loop because it would take
> significantly more time.
>
> Thanks,
> Greg
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list