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

Duncan Murdoch murdoch at stats.uwo.ca
Tue Aug 18 14:03:21 CEST 2009

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

More information about the R-help mailing list