Skip to content

How to convert Charagter variables into numeric

3 messages · Karina Knaus, Wacek Kusnierczyk, Marc Schwartz

#
You might want to try as.numeric(X), if you want to convert a factor then
you could use as.numeric(as.character(X)), where X is a "variable".

Karina
On Thu, 5 Feb 2009 22:50:38, <arup.pramanik27 at gmail.com> wrote:

            
and
into
--
#
if you use read.table for the import, reading about the colClasses
parameter in ?read.table may help.

vQ
#
There is an issue that is being overlooked here.

The read.table() family of functions use type.convert() by default
internally to determine the resultant data types of the columns going
into the data frame from the source text file. Everything read in by
read.table() starts as character and then type.convert() (unless
overridden by 'colClasses') does its job.

If a column that "should be" numeric is being converted to a factor,
then there is a data integrity problem in that column. In other words,
there are non-numeric characters in that column, preventing the column
from being converted to numeric.

For example:

# All 'numeric' source data
int [1:5] 1 2 3 4 5

# a non-numeric character in the source vector
Factor w/ 6 levels "1","2","3","4",..: 1 2 3 4 5 6


Bottom line, Arup needs to go back and review his source data to
understand why the columns that should be numeric end up as factors.

HTH,

Marc Schwartz
on 02/06/2009 07:43 AM Wacek Kusnierczyk wrote: