Re: [R] setting elements to NA across an array

From: John Fox <jfox_at_mcmaster.ca>
Date: Thu 28 Jul 2005 - 04:04:50 EST


Dear Dr Carbon,

Actually, it appears that I'm the one who was being obtuse. To make sure that I'm now interpreting what you want correctly, you'd like to set all entries in a layer to NA if any entry in a layer is NA. If that's correct, then how about the following?

foo[array(apply(foo, c(1,2), function(x) any(is.na(x))), dim(foo))] <- NA

Regards,
 John



John Fox
Department of Sociology
McMaster University
Hamilton, Ontario
Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox

> -----Original Message-----
> From: Dr Carbon [mailto:drcarbon@gmail.com]
> Sent: Wednesday, July 27, 2005 12:45 PM
> To: John Fox
> Cc: r-help@r-project.org
> Subject: Re: [R] setting elements to NA across an array
>
> Sorry for being obtuse.
>
> How can I build a logical statement that will set foo[2,2,]
> <- NA and foo[3,5,] <- NA? Something like, if any row and
> column are NA, then set NA to that row and column to NA for
> the entire array....
>
>
>
> On 7/27/05, John Fox <jfox@mcmaster.ca> wrote:
> > Dear Dr Carbon,
> >
> >
> > > -----Original Message-----
> > > From: r-help-bounces@stat.math.ethz.ch
> > > [mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of Dr Carbon
> > > Sent: Wednesday, July 27, 2005 12:19 PM
> > > To: r-help@r-project.org
> > > Subject: [R] setting elements to NA across an array
> > >
> > > Please excuse what is obviously a trivial matter...
> > >
> > > I have a large 3-d array. I wish to set the third dimension
> > > (z) to NA if there are any NA values in the first two dimnesions
> > > (xy). So, given array foo:
> > >
> > > foo <- array(data = NA, dim = c(5,5,3))
> > > foo[,,1] <- matrix(rnorm(25), 5, 5)
> > > foo[,,2] <- matrix(rnorm(25), 5, 5)
> > > foo[,,3] <- matrix(rnorm(25), 5, 5)
> > >
> > > I'll set two elements to NA
> > >
> > > foo[2,2,1]<- NA
> > > foo[3,5,2]<- NA
> > > foo
> > >
> > > Now I want to set foo[2,2,] <- NA and foo[3,5,] <- NA. How can I
> > > build a logical statement to do this?
> > >
> >
> > That should work just as you've specified it: That is,
> elements in all
> > "layers" in the second row, second column and in the the third row,
> > fifth column of the array should be NA. Since you posed the
> question,
> > I suppose that I'm missing something.
> >
> > I hope this helps,
> > John
> >
> > > ______________________________________________
> > > R-help@stat.math.ethz.ch mailing list
> > >
https://stat.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide!
> > > http://www.R-project.org/posting-guide.html
> >
> >



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Jul 28 04:09:49 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:34:06 EST