[R] assign value between different type: Double vs Integer

Sarah Goslee sarah.goslee at gmail.com
Sat Apr 24 17:02:09 CEST 2010


Ah, you didn't provide a complete description of
your problem. gdata is a factor. You need to
read about converting factors to numeric, or
perhaps reconsider your method of creating or
importing gdata.

as.numeric(somefactor) does not extract the
printed value, it extracts the *order of the factor*.

> somefactor <- as.factor(c(1,2,6,4))
> somefactor
[1] 1 2 6 4
Levels: 1 2 4 6
> as.numeric(somefactor)
[1] 1 2 4 3
> as.numeric(as.character(somefactor))
[1] 1 2 6 4
>
>
> somefactor <- as.factor(c("a", "b", "d", "c"))
> somefactor
[1] a b d c
Levels: a b c d
> as.numeric(somefactor)
[1] 1 2 4 3


Please read the posting guide, and follow the
instructions therein for providing a clear and
complete question. Plus, I think this is a FAQ.

Sarah

On Sat, Apr 24, 2010 at 10:50 AM, Dong GUO <cguous at gmail.com> wrote:
> Thanks, Sarah.
> the loop is all right. this is what I just experimented.
> and when trying to do as.numeric (ggg), and I got the 883 coming out, which
> basically is the point here. somehow in the process, it transformed the
> 10354 to 883 as a different a type from Double to Numeric...
> Dont know how this can happen..
>



-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list