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

Tom Roche Tom_Roche at pobox.com
Sun May 12 00:24:23 CEST 2013


Tom Roche Wed, 08 May 2013 20:38:12 -0400
>> 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].

Bill Dunlap Thu, 9 May 2013 04:02:18 +0000
> [assuming] that the dimensions of the grids are the same[:]

> grid1 <- c(1,2,NA,3,NA)
> grid2 <- c(101,102,103,104,NA)
> shouldCopy <- is.na(grid1) & !is.na(grid2)
> grid1[shouldCopy] <- grid2[shouldCopy]
> grid1
[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



More information about the R-help mailing list