[R] vector replacement 1/0 to P/A
Petr PIKAL
petr.pikal at precheza.cz
Tue Aug 18 15:55:05 CEST 2009
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-bounces at 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,] 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
>
> ______________________________________________
> 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.
More information about the R-help
mailing list