[R] Indexing matrix

Gabor Grothendieck ggrothendieck at gmail.com
Tue Jun 30 15:26:07 CEST 2009


On Tue, Jun 30, 2009 at 9:19 AM, Rainer M Krug<r.m.krug at gmail.com> wrote:
> Hi
>
> imagine the following situation:
>
> a <- runif(100)
> plot(a[a>0.5])
>
> plots only the elements in the vector which are larger then 0.5
>
> Now imagine a matrix:
>
> b <- matrix(runif(20000), ncol=40, nrow=50)
> image(b[b>0.5])
> Error in image.default(b[b > 0.5]) : argument must be matrix-like
>
> Is there a similar way then above to plot an image from the matrix with only
> the elements larger then 0.5, and the remaining elements equals NA?
>
> I know I could do something like
>
> image(b, zlim=c(0.5, 1))
> but this includes 0.5 in the image,
>
> bb <- b
> bb[bb<=0.5] <- NA
> image(bb)
>
> but this includes an additional assignment.

Although this does involve an assignment underneath
as well perhaps its sufficient for your purposes:

image(replace(b, b <= 0.5, NA))




More information about the R-help mailing list