[R] Converting "factors" to "numeric" in a dataframe

Petr PIKAL petr.pikal at precheza.cz
Thu Mar 18 13:04:11 CET 2010


Hi

as you did not provide data it is hard to say what is wrong.

You can see that it is working on dates similar what you described.

 test <- data.frame(x=letters[1:10], y=rnorm(10), z=runif(10))
 test
   x           y          z
1  a  0.09980806 0.32211567
2  b  0.70559139 0.32204076
3  c -1.50514354 0.28267338
............

testnum<-data.frame(t(test[,-1]))
names(testnum)<-test[,1]
testnum
           a         b          c         d         e           f g
y 0.09980806 0.7055914 -1.5051435 0.4421971 0.1041789 -1.54683799 
0.3405809
z 0.32211567 0.3220408  0.2826734 0.8819248 0.5189688  0.05171076 
0.4583101
          h          i         j
y 2.3137394 -0.4953507 0.7668954
z 0.7515886  0.5876854 0.4192073


Regards
Petr



Michael Glanville <michael.glanville19 at googlemail.com> napsal dne 
18.03.2010 12:37:21:

> Thanks Petr, your suggestion has worked to a certain extent. The only 
issue is
> that the sample names don't appear in the final dataframe. However, I 
will 
> persevere and see what I can do.
>  
> Many thanks for you invaluable help,
>  
> Mike

> On 18 March 2010 11:23, Petr PIKAL <petr.pikal at precheza.cz> wrote:
> Hi Michael
> 
> 
> r-help-bounces at r-project.org napsal dne 18.03.2010 12:02:19:
> 
> > Hi petr,
> >
> > Thanks for the reply.
> >
> > My original data is in "comma separated variable" (csv) format with
> variable
> > names in column 1 and numeric data in the remaining columns. The
> "read.csv"
> > command reads this data set into object name "pcrdata" as a dataframe
> where
> > the variable names and numeric data are conserved (as required).
> However,

> So why not transpose only numeric part, then set it to data frame and 
add
> column names from first column.
> 
> Something like (untested)
> 
> pcrdata<-read.csv("File_path",header=FALSE)

> numdata<-t(pcrdata[,-1])
> numdata<-data.frame(numdata)
> names(numdata) <- pcrdata[,1]
> 
> Regards
> Petr
>



More information about the R-help mailing list