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

Petr PIKAL petr.pikal at precheza.cz
Wed Mar 17 16:13:01 CET 2010


r-help-bounces at r-project.org napsal dne 17.03.2010 15:23:34:

> I am currently trying to write a program that minimises the amount of 
> required for “auditable” qPCR data. At the moment I am using an Excel 
> spreadsheet as source data that has been transposed to the column format
> required for R to read. Unfortunately, this means I have* *to manually
> confirm the whole data set prior to doing any analysis, which is taking 
> considerable amount of time! My idea now is to read the raw data in 
> and get R to do the transformation prior to analysis. The problem I now 
> is that, upon transposition, the data are converted to “character” in a
> matrix, rather than “factor” and “numeric” in a dataframe. I have 
> in changing the matrix to a dataframe (via as.data.frame(object)), but 
> then converts all the data to “factor” which I can’t use for my analysis
> since, other than the column headings, I need the data to be numeric. I 
> tried coercing the data to numeric using the as() and as.numeric() 
> but this has no effect on the data format. I have no experience in
> programming and so am at a loss as to what to do: am I making a basic 
> in my programming or missing something essential (or both!)?
> I am using R version 2.9.0 at the moment, but this will change as soon 
as I
> have sorted this issue out. Below is the code I have put together, as 
> can see it is VERY brief but essential to allow my analysis to proceed:
> pcrdata<-read.csv("File_path",header=FALSE)

This is supposed to be data frame already. As you did not show us any of 
possible clues of data type like


it is difficult to say.

However from your description your original data are in columns which have 
numeric and character data together which is not possible. I believe that 
there are options for reading such data.

> pcrdata<-as.data.frame(t(pcrdata))

OK. Here you say you get data in columns but they are all character.

> pcrdata[2:51]<-as.numeric(as.character(pcrdata))

Here it depends whether they are all numeric or if some of them shall be 
character (factor). Functions like those above can not be used directly on 
data frames. You need to use apply.

apply(pcrdata, 1, as.character)

Exact sequence of required functions is impossible to guess without 
knowing structure of your objects.

You shall also consult R intro and R data manuals.


> Any help would be gratefully appreciated,
> Mike Glanville
>    [[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 
> and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list