Problem with converting factors to numbers

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

Problem with converting factors to numbers

_Luc_
Hi all,
I have a very simple problem, but i cant find a solution probably because i search for the wrong keywords.

I have coordinates x and y. They look like this:

> str(data)
'data.frame':   13979 obs. of  2 variables:
 $ x               : Factor w/ 3815 levels "","186011.4971280",..: 1528 1524 1524 1524 1524 ...
 $ y               : Factor w/ 3792 levels "","449372.9599780",..: 1333 1116 1116 1116 1116 ...


> data$x[1:10]
 [1] 186843.8131840 186842.6156650 186842.6156650 186842.6156650 186842.6156650 186842.6156650 186842.6156650 186842.6156650
 [9] 186842.6156650 186842.6156650
3815 Levels:  186011.4971280 186021.0356270 186048.6246680 186051.3570180 186052.7204140 186052.9162970 ... 188132.9056840


I want them in numbers, so i run:
data$x2 <- as.numeric(data$x)

But what it did is this:
> data$x2[1:10]
 [1] 1528 1524 1524 1524 1524 1524 1524 1524 1524 1524

I tried converting it via character and then to numeric, but also that didnt work.

Anyone ideas?




Reply | Threaded
Open this post in threaded view
|

Re: Problem with converting factors to numbers

jholtman
this is an FAQ (forget which one), but the solution is:

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

Sent from my iPad

On Sep 20, 2011, at 3:40, _Luc_ <[hidden email]> wrote:

> Hi all,
> I have a very simple problem, but i cant find a solution probably because i
> search for the wrong keywords.
>
> I have coordinates x and y. They look like this:
>
>> str(data)
> 'data.frame':   13979 obs. of  2 variables:
> $ x               : Factor w/ 3815 levels "","186011.4971280",..: 1528 1524
> 1524 1524 1524 ...
> $ y               : Factor w/ 3792 levels "","449372.9599780",..: 1333 1116
> 1116 1116 1116 ...
>
>
>> data$x[1:10]
> [1] 186843.8131840 186842.6156650 186842.6156650 186842.6156650
> 186842.6156650 186842.6156650 186842.6156650 186842.6156650
> [9] 186842.6156650 186842.6156650
> 3815 Levels:  186011.4971280 186021.0356270 186048.6246680 186051.3570180
> 186052.7204140 186052.9162970 ... 188132.9056840
>
>
> I want them in *numbers*, so i run:
> data$x2 <- as.numeric(data$x)
>
> But what it did is this:
>> data$x2[1:10]
> [1] 1528 1524 1524 1524 1524 1524 1524 1524 1524 1524
>
> I tried converting it via character and then to numeric, but also that didnt
> work.
>
> Anyone ideas?
>
>
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Problem-with-converting-factors-to-numbers-tp3826061p3826061.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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.

______________________________________________
[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: Problem with converting factors to numbers

PIKAL Petr
In reply to this post by _Luc_
Hi
>
> [R] Problem with converting factors to numbers
>
> Hi all,
> I have a very simple problem, but i cant find a solution probably
because i
> search for the wrong keywords.
>
> I have coordinates x and y. They look like this:
>
> > str(data)
> 'data.frame':   13979 obs. of  2 variables:
>  $ x               : Factor w/ 3815 levels "","186011.4971280",..: 1528
1524
> 1524 1524 1524 ...
>  $ y               : Factor w/ 3792 levels "","449372.9599780",..: 1333
1116
> 1116 1116 1116 ...
>
>
> > data$x[1:10]
>  [1] 186843.8131840 186842.6156650 186842.6156650 186842.6156650
> 186842.6156650 186842.6156650 186842.6156650 186842.6156650
>  [9] 186842.6156650 186842.6156650
> 3815 Levels:  186011.4971280 186021.0356270 186048.6246680
186051.3570180

> 186052.7204140 186052.9162970 ... 188132.9056840
>
>
> I want them in *numbers*, so i run:
> data$x2 <- as.numeric(data$x)
>
> But what it did is this:
> > data$x2[1:10]
>  [1] 1528 1524 1524 1524 1524 1524 1524 1524 1524 1524
>
> I tried converting it via character and then to numeric, but also that
didnt
> work.

It shall work. However nobody knows how did you apply converting via
character and numeric until you post the code you actually used.

>From str it seems to me that either your data are from beginning non
numeric or you have some non numeric characters in your input data.

Regards
Petr

>
> Anyone ideas?
>
>
>
>
>
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/Problem-with-
> converting-factors-to-numbers-tp3826061p3826061.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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.

______________________________________________
[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: Problem with converting factors to numbers

_Luc_
This post was updated on .
In reply to this post by _Luc_
ok great, as.numeric(as.character(data$x)) works perfectly.

I am sorry that i did not find this before making this post.


@ petr
what did not work was:
data$x2 <- as.character(data$x)
data$x3 <- as.numeric(data$x2)
Reply | Threaded
Open this post in threaded view
|

Re: Problem with converting factors to numbers

Duncan Murdoch-2
On 11-09-20 7:25 AM, _Luc_ wrote:
> ok great, as.numeric(as.character(data$x)) works perfectly.
>
> I am sorry that i did not find this before making this post.
>
>
> @ peter
> what did not work was:
> data$x2<- as.character(data$x)
> data$x3<- as.numeric(data$x2)

That should have worked.  Can you put together a simple example where it
doesn't?

Duncan Murdoch

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