[R] vector replacement 1/0 to P/A

2009-08-18 Thread Lana Schaffer
Hi, Can someone suggest an efficient way to substitute a vector/matrix which contains 1's and 0's to P's and A's (resp.)? Thanks, Lana __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide

Re: [R] vector replacement 1/0 to P/A

2009-08-18 Thread Chuck Cleland
On 8/17/2009 10:22 AM, Lana Schaffer wrote: Hi, Can someone suggest an efficient way to substitute a vector/matrix which contains 1's and 0's to P's and A's (resp.)? Thanks, Lana Here is one approach: mymat - matrix(rbinom(15, 1, .5), ncol=3) mymat [,1] [,2] [,3] [1,]100

Re: [R] vector replacement 1/0 to P/A

2009-08-18 Thread Duncan Murdoch
On 17/08/2009 10:22 AM, Lana Schaffer wrote: Hi, Can someone suggest an efficient way to substitute a vector/matrix which contains 1's and 0's to P's and A's (resp.)? x[x == 1] - P x[x == 0] - A (I added the quotes around 0 on the second line because the first line changed x to a character

Re: [R] vector replacement 1/0 to P/A

2009-08-18 Thread jim holtman
Will this do it: x - sample(0:1,10,TRUE) x [1] 1 0 0 1 0 1 0 0 0 0 ifelse(x == 1, P, A) [1] P A A P A P A A A A On Mon, Aug 17, 2009 at 10:22 AM, Lana Schafferschaf...@scripps.edu wrote: Hi, Can someone suggest an efficient way to substitute a vector/matrix which contains 1's and 0's

Re: [R] vector replacement 1/0 to P/A

2009-08-18 Thread Petr PIKAL
Hi As matrix is vector with dim attribute mymat-ifelse(mymat==0, A,P) should be sufficient. Even with data frame it works mydf-data.frame(mymat) mydf-ifelse(mydf==0, A,P) mydf X1 X2 X3 [1,] P P A [2,] A A A [3,] A A P [4,] A A P [5,] P P P Regards Petr