[R] vector replacement 1/0 to P/A
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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] vector replacement 1/0 to P/A
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 [2,]001 [3,]101 [4,]010 [5,]110 mymat[] - sapply(mymat, function(x){ifelse(x == 1, 'P', ifelse(x == 0, 'A', NA))}) mymat [,1] [,2] [,3] [1,] P A A [2,] A A P [3,] P A P [4,] A P A [5,] P P A __ R-help@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. -- Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894 __ R-help@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.
Re: [R] vector replacement 1/0 to P/A
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 vector. This isn't necessary, 0 == 0 comes out TRUE, but I think it is clearer.) Duncan Murdoch __ R-help@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.
Re: [R] vector replacement 1/0 to P/A
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 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 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? __ R-help@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.
Re: [R] vector replacement 1/0 to P/A
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 r-help-boun...@r-project.org napsal dne 18.08.2009 13:58:51: 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 [2,]001 [3,]101 [4,]010 [5,]110 mymat[] - sapply(mymat, function(x){ifelse(x == 1, 'P', ifelse(x == 0, 'A', NA))}) mymat [,1] [,2] [,3] [1,] P A A [2,] A A P [3,] P A P [4,] A P A [5,] P P A __ R-help@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. -- Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894 __ R-help@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. __ R-help@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.