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

Bert Gunter gunter.berton at gene.com
Sun May 12 02:05:09 CEST 2013


(Private)

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]
>> 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.

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
> *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
>
> ______________________________________________
> 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.



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm



More information about the R-help mailing list