On Mon, Aug 11, 2008 at 08:23:19AM +1200, Gareth Campbell
wrote:> Hey team,
>
> If I have a matrix:
>
> 1, 2,
> 3, 4,
> 4, 0,
> 1, 3,
> 0, 3
>
> 2 columns.
>
> I want to write an if command that looks at (in this case) row 3 and looks
> to see if either [3,1] or [3,2] has a zero in it. IF it does have a zero I
> want the zero to be placed in another matrix in the same position. I know
> how to do the latter part, I just can't get the if command to look at
both
> cells and deal with them separately.
I think you want to be using ifelse():
> a
[,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 4 0
[4,] 1 3
[5,] 0 3> b
[,1] [,2]
[1,] 9 9
[2,] 9 9
[3,] 9 9
[4,] 9 9
[5,] 9 9> ifelse(a == 0, a, b) ## do the whole thing
[,1] [,2]
[1,] 9 9
[2,] 9 9
[3,] 9 0
[4,] 9 9
[5,] 0 9> b[3,] <- ifelse(a[3,] == 0, a[3,], b[3,]) ## just row 3
> b
[,1] [,2]
[1,] 9 9
[2,] 9 9
[3,] 9 0
[4,] 9 9
[5,] 9 9
Dan
>
> Thanks
>
> --
> Gareth Campbell
> PhD Candidate
> The University of Auckland
>
> P +649 815 3670
> M +6421 256 3511
> E gareth.campbell at esr.cri.nz
> gcam032 at gmail.com
>
> [[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.