Re: [R] convert characters into integers in a matrix

From: Henrique Dallazuanna <wwwhsd_at_gmail.com>
Date: Tue, 10 Jun 2008 17:28:55 -0300

Try this:

x <- matrix(c("A", "P", "M", "A", "P", "M"), 2) x[] <- as.numeric(as.character(factor(x, labels = c(0,1,1)))) x

On 6/10/08, Manisha Brahmachary <mb3058_at_c2b2.columbia.edu> 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.
>

-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

	[[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.

Received on Tue 10 Jun 2008 - 20:53:22 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 10 Jun 2008 - 21:30:58 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive