Skip to content

[newbie] *apply to matching elements of n arrays?

5 messages · Jeff Newmiller, William Dunlap, Tom Roche +1 more

#
How to apply a function to all elements with the same indices in
multiple arrays? E.g.:

I have two spatial grids defined the same way (i.e., same number of
rows and columns--and dimensions, both 2D). Wherever both

* the value of an element i,j in the first grid is NA
* the value of element i,j in the second grid is !NA

I want to copy the value from grid2[i,j] to grid1[i,j]. These matrices
are not too big, so I'm able to do this with loops, but I know that's
not "the R way." How to parallelize/vectorize this, e.g., with a
single call to an 'apply'-type method? I believe I know how to operate
on a single matrix (e.g., by using `apply` to traverse it by rows or
cols), but not how to operate on multiple matrices.

Extra points for solutions that generalize to 3D or 4D (i.e., that allow
applying the same function to identical elements in n arrays of n
dimensions), since I will almost certainly need to work on arrays of
those dimensions eventually.

Apologies if this is a FAQ, but a fair amount of googling via
rseek.org is not finding an answer (perhaps because I'm not using the
correct search terms). Feel free (in fact, be encouraged :-) to reply
directly to me as well as the list (I'm on the digest, which gets huge).

TIA, Tom Roche <Tom_Roche at pobox.com>
#
Not particularly interested in "points" from you. Would like reproducibility from you [1], including dput of before and after data, and (inefficient) code of course.

 For loops are part of R.. If your data structure is not set up to take advantage of vectorization, then for loops are generally of the same order of speed as apply functions, and are perfectly usable. You may benefit from sensible memory management optimizations, or from a data reorganization so you can do vector computations.

[1] http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.
Tom Roche <Tom_Roche at pobox.com> wrote:

            
#
Is the following what you want?   It works for vectors or arrays of
any number of dimensions.  It assumes that the dimensions of the
grids are the same.
[1]   1   2 103   3  NA

You don't really need the ' & !is.na(grid2)': it just stops the copying
of NA elements of grid2 to NA elements of grid1.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
2 days later
#
Tom Roche Wed, 08 May 2013 20:38:12 -0400
Bill Dunlap Thu, 9 May 2013 04:02:18 +0000
[1]   1   2 103   3  NA

Thanks! I had no idea bitwise booleans (which are what come to my mind
when I see '&' or '|') were overloaded thus. Much more convenient than
*apply semantics for this task! Variants of the above now in use @

https://bitbucket.org/tlroche/aqmeii_ag_soil/src/c5fc8038281d0d5683d45bc797c59fc4b0a55c6b/combine_EDGAR_and_EPIC_emissions.r?at=master
#
(Private)
On Sat, May 11, 2013 at 3:24 PM, Tom Roche <Tom_Roche at pobox.com> wrote:
But you would have if you had read the Intro to R tutorial that ships
with R, or other online tutorial of your choice. Please make the
effort.

-- Bert

 Much more convenient than