Factor to Numeric

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Factor to Numeric

mohan L
Dear All,

I have a data frame with State and 12 Months as column. I want to convert
all the 12 month column from factor to numeric.
any help will be greatly appreciated.
> str(data)
'data.frame':    33 obs. of  9 variables:
 $ State    : Factor w/ 33 levels "Andaman and Nicobar Islands",..: 1 2 3 4
5 6 7 8 9 10 ...
 $ April    : Factor w/ 21 levels "0","1","10","116",..: 2 16 1 1 9 20 1 1 2
21 ...
 $ August   : Factor w/ 18 levels "0","1","10","125",..: 1 14 1 1 9 3 1 1 2
5 ...
 $ February : Factor w/ 19 levels "0","1","103",..: 1 15 1 1 2 6 2 1 2 14
...
 $ January  : Factor w/ 19 levels "0","1","10","114",..: 1 15 1 1 11 12 2 1
1 4 ...
 $ July     : Factor w/ 16 levels "0","1","122",..: 1 13 1 1 1 12 8 2 1 4
...
 $ June     : Factor w/ 18 levels "0","1","10","119",..: 1 14 1 1 9 10 9 2 1
4 ...
 $ May      : Factor w/ 20 levels "0","1","10","111",..: 2 19 1 1 10 15 1 1
1 5 ...
 $ September: Factor w/ 19 levels "0","1","11","122",..: 1 18 1 1 14 3 2 1 2
9 ...

Thanks & Rg
Mohan L

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Factor to Numeric

Ivan Calandra
Hi,

What about:
as.numeric(as.character(data$State))
?

Ivan

Le 5/26/2010 09:32, Mohan L a écrit :

> Dear All,
>
> I have a data frame with State and 12 Months as column. I want to convert
> all the 12 month column from factor to numeric.
> any help will be greatly appreciated.
>    
>> str(data)
>>      
> 'data.frame':    33 obs. of  9 variables:
>   $ State    : Factor w/ 33 levels "Andaman and Nicobar Islands",..: 1 2 3 4
> 5 6 7 8 9 10 ...
>   $ April    : Factor w/ 21 levels "0","1","10","116",..: 2 16 1 1 9 20 1 1 2
> 21 ...
>   $ August   : Factor w/ 18 levels "0","1","10","125",..: 1 14 1 1 9 3 1 1 2
> 5 ...
>   $ February : Factor w/ 19 levels "0","1","103",..: 1 15 1 1 2 6 2 1 2 14
> ...
>   $ January  : Factor w/ 19 levels "0","1","10","114",..: 1 15 1 1 11 12 2 1
> 1 4 ...
>   $ July     : Factor w/ 16 levels "0","1","122",..: 1 13 1 1 1 12 8 2 1 4
> ...
>   $ June     : Factor w/ 18 levels "0","1","10","119",..: 1 14 1 1 9 10 9 2 1
> 4 ...
>   $ May      : Factor w/ 20 levels "0","1","10","111",..: 2 19 1 1 10 15 1 1
> 1 5 ...
>   $ September: Factor w/ 19 levels "0","1","11","122",..: 1 18 1 1 14 3 2 1 2
> 9 ...
>
> Thanks&  Rg
> Mohan L
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>
>    

--
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
[hidden email]

**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Factor to Numeric

mohan L
On Wed, May 26, 2010 at 1:13 PM, Ivan Calandra <[hidden email]
> wrote:

> Hi,
>
> What about:
> as.numeric(as.character(data$State))
> ?
>

What I what is, I want to convert all the column excluding State to factor
to numeric in the data frame. So that  I will send  this data frame to
another function to total .

Any help.

Thanks & Rg
Mohan L

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Factor to Numeric

Ivan Calandra
What about (not tested because no sample data, and I'm definitely unsure
of my lapply() solution):
lapply(data[2:13], FUN=as.character)
lapply(data[2:13], FUN=as.numeric)
?

data[2:13] should correspond to the 12 Month columns.
There might be a way to combine both lines but I don't think lapply
would work with 2 function names.

When I can't find a vectorized solution, or one with *apply, I use a for
loop. I find it very easy to understand and it's fast enough for my
"small" datasets

HTH
Ivan

Le 5/26/2010 09:57, Mohan L a écrit :

>
>
> On Wed, May 26, 2010 at 1:13 PM, Ivan Calandra
> <[hidden email] <mailto:[hidden email]>>
> wrote:
>
>     Hi,
>
>     What about:
>     as.numeric(as.character(data$State))
>     ?
>
>
> What I what is, I want to convert all the column excluding State to
> factor to numeric in the data frame. So that  I will send  this data
> frame to another function to total .
>
> Any help.
>
> Thanks & Rg
> Mohan L
>
--
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
[hidden email]

**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php


        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Factor to Numeric

David Winsemius

On May 26, 2010, at 4:05 AM, Ivan Calandra wrote:

> What about (not tested because no sample data, and I'm definitely  
> unsure
> of my lapply() solution):
> lapply(data[2:13], FUN=as.character)
> lapply(data[2:13], FUN=as.numeric)
> ?
>

That would not preserve any of the output since no assignments were  
made. Consider instead:

data[ , -1] <- lapply(data[, -1] , function(x)  
as.numeric(as.character(x))  )

Would be safer to make a copy of "data" and work on it, and of course,  
there is always:

require(fortunes)
fortune("dog")

--
David.

> data[2:13] should correspond to the 12 Month columns.
> There might be a way to combine both lines but I don't think lapply
> would work with 2 function names.
>
> When I can't find a vectorized solution, or one with *apply, I use a  
> for
> loop. I find it very easy to understand and it's fast enough for my
> "small" datasets
>
> HTH
> Ivan
>
> Le 5/26/2010 09:57, Mohan L a écrit :
>>
>>
>> On Wed, May 26, 2010 at 1:13 PM, Ivan Calandra
>> <[hidden email] <mailto:[hidden email]>>
>> wrote:
>>
>>    Hi,
>>
>>    What about:
>>    as.numeric(as.character(data$State))
>>    ?
>>
>>
>> What I what is, I want to convert all the column excluding State to
>> factor to numeric in the data frame. So that  I will send  this data
>> frame to another function to total .
>>
>> Any help.
>>
>> Thanks & Rg
>> Mohan L
>>
>
> --
> Ivan CALANDRA
> PhD Student
> University of Hamburg
> Biozentrum Grindel und Zoologisches Museum
> Abt. Säugetiere
> Martin-Luther-King-Platz 3
> D-20146 Hamburg, GERMANY
> +49(0)40 42838 6231
> [hidden email]
>
> **********
> http://www.for771.uni-bonn.de
> http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.