Re: [R] factor levels numeric values

2015-02-11 Thread JS Huang
Hi,

  Suppose your data frame is called data and the name of the factor column
is named tobeConverted.  I have tried this and it worked.  Hope this helps.

 as.numeric(as.character(data$tobeConverted))



--
View this message in context: 
http://r.789695.n4.nabble.com/factor-levels-numeric-values-tp4699515p4703090.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] factor levels numeric values

2014-11-12 Thread David Studer
Hi everybody,

I have another question (to which I could not find an answer in my r-books.
I am sure, it's not a great issue, but I simply lack of a good idea how to
solve this:

One of my variables gets imported as a factor instead of a numeric variable.
Now I have a...
 Factor w/ 63 levels 0,0.02,0.03,..: 1 NA NA 1 NA NA 1 1 53 10 ...

How can I transform these factor levels into actual values?

Thank you very much for any help!
David

[[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.


Re: [R] factor levels numeric values

2014-11-12 Thread Gerrit Eichner

Hello, David,

take a look at the beginning of the Warning section of ?factor.

 Hth  --  Gerrit


Hi everybody,

I have another question (to which I could not find an answer in my r-books.
I am sure, it's not a great issue, but I simply lack of a good idea how to
solve this:

One of my variables gets imported as a factor instead of a numeric variable.
Now I have a...
Factor w/ 63 levels 0,0.02,0.03,..: 1 NA NA 1 NA NA 1 1 53 10 ...

How can I transform these factor levels into actual values?

Thank you very much for any help!
David

[[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-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.


Re: [R] factor levels numeric values

2014-11-12 Thread David L Carlson
Also look at the Frequently Asked Questions document that comes with your R 
installation:

7.10 How do I convert factors to numeric?

It may happen that when reading numeric data into R (usually, when reading in a 
file), they come in as factors. If f is such a factor object, you can use

as.numeric(as.character(f))

to get the numbers back. More efficient, but harder to remember, is

as.numeric(levels(f))[as.integer(f)]

In any case, do not call as.numeric() or their likes directly for the task at 
hand (as as.numeric() or unclass() give the internal codes).

-
David L Carlson
Department of Anthropology
Texas AM University
College Station, TX 77840-4352

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Gerrit Eichner
Sent: Wednesday, November 12, 2014 8:06 AM
To: David Studer
Cc: r-help@r-project.org
Subject: Re: [R] factor levels  numeric values

Hello, David,

take a look at the beginning of the Warning section of ?factor.

  Hth  --  Gerrit

 Hi everybody,

 I have another question (to which I could not find an answer in my r-books.
 I am sure, it's not a great issue, but I simply lack of a good idea how to
 solve this:

 One of my variables gets imported as a factor instead of a numeric variable.
 Now I have a...
 Factor w/ 63 levels 0,0.02,0.03,..: 1 NA NA 1 NA NA 1 1 53 10 ...

 How can I transform these factor levels into actual values?

 Thank you very much for any help!
 David

   [[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-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-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.


Re: [R] factor levels numeric values

2014-11-12 Thread Ivan Calandra
I have not completely followed the discussion, so excuse me if it was 
already pointed out.
If numeric data are read as factors, this means that there are not only 
numeric data in the column. It could be an empty space somewhere, or 
some character that should be NA, or...
I think it is worth spending some time searching for the typo so that 
the file will be read correctly in R.


HTH,
Ivan

--
Ivan Calandra, ATER
University of Reims Champagne-Ardenne
GEGENA² - EA 3795
CREA - 2 esplanade Roland Garros
51100 Reims, France
+33(0)3 26 77 36 89
ivan.calan...@univ-reims.fr
https://www.researchgate.net/profile/Ivan_Calandra

Le 12/11/14 15:56, David L Carlson a écrit :

Also look at the Frequently Asked Questions document that comes with your R 
installation:

7.10 How do I convert factors to numeric?

It may happen that when reading numeric data into R (usually, when reading in a 
file), they come in as factors. If f is such a factor object, you can use

as.numeric(as.character(f))

to get the numbers back. More efficient, but harder to remember, is

as.numeric(levels(f))[as.integer(f)]

In any case, do not call as.numeric() or their likes directly for the task at 
hand (as as.numeric() or unclass() give the internal codes).

-
David L Carlson
Department of Anthropology
Texas AM University
College Station, TX 77840-4352

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Gerrit Eichner
Sent: Wednesday, November 12, 2014 8:06 AM
To: David Studer
Cc: r-help@r-project.org
Subject: Re: [R] factor levels  numeric values

Hello, David,

take a look at the beginning of the Warning section of ?factor.

   Hth  --  Gerrit


Hi everybody,

I have another question (to which I could not find an answer in my r-books.
I am sure, it's not a great issue, but I simply lack of a good idea how to
solve this:

One of my variables gets imported as a factor instead of a numeric variable.
Now I have a...
Factor w/ 63 levels 0,0.02,0.03,..: 1 NA NA 1 NA NA 1 1 53 10 ...

How can I transform these factor levels into actual values?

Thank you very much for any help!
David

[[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-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-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-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.


Re: [R] factor levels numeric values

2014-11-12 Thread Jeff Newmiller
Another approach is to re-import your data using options that do not put the 
data into a factor in the first place.  For example you can use the colClasses 
parameter in the read.table family of functions to specify numeric for that 
column. If you need to give special handling to that column anyway (using 
strong functions) then you can use the stringsAsFactors=FALSE or as.is=TRUE 
parameter settings and avoid the as.character() band-aid in your code.
---
Jeff NewmillerThe .   .  Go Live...
DCN:jdnew...@dcn.davis.ca.usBasics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

On November 12, 2014 6:56:11 AM PST, David L Carlson dcarl...@tamu.edu wrote:
Also look at the Frequently Asked Questions document that comes with
your R installation:

7.10 How do I convert factors to numeric?

It may happen that when reading numeric data into R (usually, when
reading in a file), they come in as factors. If f is such a factor
object, you can use

as.numeric(as.character(f))

to get the numbers back. More efficient, but harder to remember, is

as.numeric(levels(f))[as.integer(f)]

In any case, do not call as.numeric() or their likes directly for the
task at hand (as as.numeric() or unclass() give the internal codes).

-
David L Carlson
Department of Anthropology
Texas AM University
College Station, TX 77840-4352

-Original Message-
From: r-help-boun...@r-project.org
[mailto:r-help-boun...@r-project.org] On Behalf Of Gerrit Eichner
Sent: Wednesday, November 12, 2014 8:06 AM
To: David Studer
Cc: r-help@r-project.org
Subject: Re: [R] factor levels  numeric values

Hello, David,

take a look at the beginning of the Warning section of ?factor.

  Hth  --  Gerrit

 Hi everybody,

 I have another question (to which I could not find an answer in my
r-books.
 I am sure, it's not a great issue, but I simply lack of a good idea
how to
 solve this:

 One of my variables gets imported as a factor instead of a numeric
variable.
 Now I have a...
 Factor w/ 63 levels 0,0.02,0.03,..: 1 NA NA 1 NA NA 1 1 53 10
...

 How can I transform these factor levels into actual values?

 Thank you very much for any help!
 David

  [[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-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-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-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.