[R] vector replacement 1/0 to P/A
Chuck Cleland
ccleland at optonline.net
Tue Aug 18 13:58:51 CEST 2009
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,] 1 0 0
[2,] 0 0 1
[3,] 1 0 1
[4,] 0 1 0
[5,] 1 1 0
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 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.
--
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
More information about the R-help
mailing list