Re: [R] Converting factors to numeric in a dataframe

2010-03-18 Thread Michael Glanville
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,
when I transpose the table it becomes a matrix and all the data become
character. Using the as.data.frame command coerces the matrix to a
dataframe but, at the same time, changes all the data to factor. As far as
I can see, for my analysis I need to coerce the data in the matrix, formed
by transposition of the raw data, to factor in row 1 and numeric for the
remaining rows before then converting the matrix to a dataframe, using the
as.data.frame command. Is this the information you required and, if so, is
what I am asking at all possible?

Best wishes,

Mike

On 17 March 2010 15:13, Petr PIKAL petr.pi...@precheza.cz wrote:

 Hi

 r-help-boun...@r-project.org napsal dne 17.03.2010 15:23:34:

  I am currently trying to write a program that minimises the amount of
 work
  required for “auditable” qPCR data. At the moment I am using an Excel
 (.csv)
  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
 a
  considerable amount of time! My idea now is to read the raw data in
 directly
  and get R to do the transformation prior to analysis. The problem I now
 have
  is that, upon transposition, the data are converted to “character” in a
  matrix, rather than “factor” and “numeric” in a dataframe. I have
 succeeded
  in changing the matrix to a dataframe (via as.data.frame(object)), but
 this
  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
 have
  tried coercing the data to numeric using the as() and as.numeric()
 commands,
  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
 error
  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
 you
  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

 str(pcrdata)

 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.

 Regards
 Petr


 
 
  Any help would be gratefully appreciated,
 
 
 
  Mike Glanville
 
 [[alternative HTML version deleted]]
 
  __
  R-help@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.htmlhttp://www.r-project.org/posting-guide.html
  and provide commented, minimal, self-contained, reproducible code.



[[alternative HTML version deleted]]

__
R-help@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.


[R] Converting factors to numeric in a dataframe

2010-03-17 Thread Michael Glanville
I am currently trying to write a program that minimises the amount of work
required for “auditable” qPCR data. At the moment I am using an Excel (.csv)
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 a
considerable amount of time! My idea now is to read the raw data in directly
and get R to do the transformation prior to analysis. The problem I now have
is that, upon transposition, the data are converted to “character” in a
matrix, rather than “factor” and “numeric” in a dataframe. I have succeeded
in changing the matrix to a dataframe (via as.data.frame(object)), but this
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 have
tried coercing the data to numeric using the as() and as.numeric() commands,
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 error
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 you
can see it is VERY brief but essential to allow my analysis to proceed:



pcrdata-read.csv(File_path,header=FALSE)

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

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



Any help would be gratefully appreciated,



Mike Glanville

[[alternative HTML version deleted]]

__
R-help@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.