[R] Converting to mode numeric within a matrix

R Help rhelp.stats at gmail.com
Fri Sep 28 16:46:28 CEST 2007

Your problem is that a matrix can't be a mix of data types, but a data
frame can.  Try the following code.

newBest = as.data.frame(best)

Now you can convert the column of numbers in newBest to numbers from
the factors they start as.  Check the help file for data.frame(...)
for more details of how to label correctly.


On 9/28/07, Sergey Goriatchev <sergeyg at gmail.com> wrote:
> Hello,
> I create a matrix:
> best <- matrix(0, ncol=2, nrow=num.selected,
> dimnames=list(the.best$.Name, c("Probability(%)", "Inside")))
> best[,1] <- as.numeric(the.best$Total*100)
> best[,2] <- ifelse(the.best$Weight==0, "No", "Yes")
> What I want is the second column of mode numeric, but it is of mode
> character, despite the attempt to convert it to numeric with
> as.numeric(). It must have something to do with the second column of
> the matrix, where I write No/Yes.
> What should I do to have the first column of mode numeric?
> Also, how can I output No/Yes in the second column without citation
> marks around them?
> Thanks in advance
> Sergey
> ______________________________________________
> 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