[R] Using apply on a three dimensional matrix and passing multiple arguments to user defined function

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Wed Sep 7 15:33:06 CEST 2016


Should be working, so the devil is in the details you are not showing. Please provide a reproducible (self-contained) example [1] and post in plain text rather than HTML formatted email to avoid code corruption. 

[1] http://adv-r.had.co.nz/Reproducibility.html
-- 
Sent from my phone. Please excuse my brevity.

On September 6, 2016 9:33:35 PM PDT, Justin Peter <Justin.Peter at usq.edu.au> wrote:
>Dear R-user,
>
>I have a three-dimensional matrix of atmospheric data. The first two
>dimensions are spatial (lon and lat) and the third is time, such that
>
>dim(data) <- c(nlon,nlat,ntime)
>
>I wish to apply a land sea mask data which is a matrix of "0" and "1"
>if dim(nlon,nlat)
>
>dim(lsmask) <- c(nlon,nlat)
>
>I wish to set all of the elements in the two-dimensional array of
>data[,,ntime] for every 1:length(ntime).
>
>I could do this in a loop:
>
>for (i in 1:ntime){
>    data[,,i][which(lsmask == 0)] <- NA
>}
>
>I would like to do this using apply, but I need to pass two variables
>to the function in apply (data and lsmask), where data is a
>two-dimensional array.
>
>I tried:
>
>mask <- function(x,y) {x[which(y==0)] <- NA}
>
>masked_data <- apply(data,c(1,2),mask,y=lsmask)
>
>but I get back a vector of dim(nlon,nlat) populated with NA.
>
>Any clues as to what I am missing?
>
>Thanks in advance for you help.
>
>Kind regards,
>Justin
>
>
>
>--
>Justin Peter
>Research Fellow
>International Centre for Applied Climate Sciences,
>University of Southern Queensland
>West St, Toowoomba, QLD, 4350
>Australia
>
>Email: justin.peter at usq.edu.au<mailto:justin.peter at usq.edu.au>
>Ph: +61 (0) 7 4631 1181
>Fax: +61 (0) 7 4631 5581
>Mob: +61 (0)474 774 107
>
>
>
>
>_____________________________________________________________
>This email (including any attached files) is confidential and is for
>the intended recipient(s) only. If you received this email by mistake,
>please, as a courtesy, tell the sender, then delete this email.
>
>The views and opinions are the originator's and do not necessarily
>reflect those of the University of Southern Queensland. Although all
>reasonable precautions were taken to ensure that this email contained
>no viruses at the time it was sent we accept no liability for any
>losses arising from its receipt.
>
>The University of Southern Queensland is a registered provider of
>education with the Australian Government.
>(CRICOS Institution Code QLD 00244B / NSW 02225M, TEQSA PRV12081 )
>
>
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>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.



More information about the R-help mailing list