[R] convert characters into integers in a matrix

Manisha Brahmachary mb3058 at c2b2.columbia.edu
Tue Jun 10 22:27:32 CEST 2008


Thanks,
This works fine.

-----Original Message-----
From: Erik Iverson [mailto:iverson at biostat.wisc.edu] 
Sent: Tuesday, June 10, 2008 4:19 PM
To: Manisha Brahmachary
Cc: r-help at r-project.org
Subject: Re: [R] convert characters into integers in a matrix

Manisha -

Will this work instead?

if 'a' is your matrix, try

ifelse(a == "A", 0, 1)

Best,
Erik Iverson

Manisha Brahmachary wrote:
> Hello,
> 
>  
> 
> I will appreciate any suggestion for this simple problem. I have a matrix
of
> characters "A", "P", "M".
> 
>  
> 
> I want to convert those characters to integers 0,1, 1 respectively. I am
> using the following R statements:
> 
>  
> 
> exprs.new<-gsub("P",1,exprs)
> 
>  exprs.new1 <- gsub("A",0,exprs.new)
> 
>  exprs.new2 <- gsub("M",1,exprs.new1)
> 
>  
> 
> The replaced integers are stored as characters and I cannot find a way to
> convert the matrix to an integer matrix.
> 
>  
> 
> Thanks
> 
> manisha
> 
>  
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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