[R] print matrix values and if statement

jim holtman jholtman at gmail.com
Mon Sep 13 14:59:14 CEST 2010


Here is how to get the indices of the non-zero values.  You can
determine how you want to print them

> x <- rbind(c(1,0), c(0,3))
> # get indices of non-zero
> which(x != 0, arr.ind=TRUE)
     row col
[1,]   1   1
[2,]   2   2

possible ways of printing:

> x.1[x.1 == 0] <- NA
> x.1
     [,1] [,2]
[1,]    1   NA
[2,]   NA    3
> print(x.1, na.print='.')
     [,1] [,2]
[1,]    1    .
[2,]    .    3



On Mon, Sep 13, 2010 at 7:44 AM, Alaios <alaios at yahoo.com> wrote:
> Hello everyone,
> I have a 2x2 matrix filled with zeros and some more values around zeros. I would
> like to print only the non-zero values
>
> and
> to keep the coords of the places that the values are not zero.
>
> Could you please help me with that?
>
> I would like to thank you in advance for your help
> Best Regards
> Alex
>
>
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list