[R] [newbie] *apply to matching elements of n arrays?
gunter.berton at gene.com
Sun May 12 02:05:09 CEST 2013
On Sat, May 11, 2013 at 3:24 PM, Tom Roche <Tom_Roche at pobox.com> wrote:
> 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]
>  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.
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
Much more convenient than
> *apply semantics for this task! Variants of the above now in use @
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Genentech Nonclinical Biostatistics
Internal Contact Info:
More information about the R-help