Manisha Brahmachary
2008-Jun-10 18:28 UTC
[R] convert characters into integers in a matrix
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]]
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.
Henrique Dallazuanna
2008-Jun-10 20:28 UTC
[R] convert characters into integers in a matrix
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@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@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]]