Re: [R] Convert a factor to a numeric

2013-10-10 Thread arun
Also, BTW, dat.num() is matrix, but if you use lapply(), it is still a dataframe.  Anyway, it depends on what the OP really wants as output. dat.num <- apply(dat, 2, FUN = function(x) as.numeric(as.character(x)))  dat[] <- lapply(dat,function(x) as.numeric(as.character(x)))  str(dat) 'data.fram

Re: [R] Convert a factor to a numeric

2013-10-10 Thread Charles Determan Jr
Firstly, please make sure to reply-all so the r-help list also receives these emails. Second, I have just run this sequence as it provides an exact copy with each as numeric. Use the apply function, it iterates over each column and converts each to numeric. dat <- read.table(text="a

Re: [R] Convert a factor to a numeric

2013-10-10 Thread Charles Determan Jr
I'm not honestly sure why data.matrix didn't work off hand. Perhaps another user can shed some light on this. An alternative is the following: apply(dat, 2, FUN = function(x) as.numeric(as.character(x))) On Thu, Oct 10, 2013 at 8:26 AM, arun wrote: > Did you mean to apply it like this or is

Re: [R] Convert a factor to a numeric

2013-10-10 Thread Charles Determan Jr
data.matrix() should do the job for you Charles On Thu, Oct 10, 2013 at 8:02 AM, arun wrote: > Hi, > It is not clear whether all the variables are factor or only a few are.. > > dat<- read.table(text="acoef > coef.l coef.h > 1 1 0.005657825001254 0.00300612956

Re: [R] Convert a factor to a numeric

2013-10-10 Thread arun
Hi, It is not clear whether all the variables are factor or only a few are.. dat<- read.table(text="a    coef   coef.l  coef.h 1   1   0.005657825001254  0.00300612956318132 0.00830952043932667 2   2 0.00634505314577229  0.00334102345418614 0.00934908283735844

Re: [R] Convert a factor to a numeric

2013-10-10 Thread Carl Witthoft
foo <- as.numeric(as.character(your_factors) ). It's a common mistake to forget the first conversion, in which case you end up with an integer sequence rather than the desired values. -- View this message in context: http://r.789695.n4.nabble.com/Convert-a-factor-to-a-numeric-tp4677964p4677971