[R] Question about levels/as.numeric

Petr Savicky savicky at praha1.ff.cuni.cz
Sun Apr 10 23:22:29 CEST 2011


On Sun, Apr 10, 2011 at 05:47:59PM +0200, Thibault Vatter wrote:
> Hi,
> 
> I am still new to R and this is my first post on this mailing-list.
> 
> I have two .csv (each one being a column of real numbers) coming from the
> same database (the first one is just longer than the second) and I read them
> in R the following way:
> 
> returns  <- read.csv("test.csv", header = FALSE)
> returns2  <- read.csv("test2.csv", header = FALSE)
> 
> However, the two objects clearly don't seem to be equivalent:
> 
> > returns[2528:2537,1]
>  [1] -0.002206 0.115696  -0.015192 0.008719  -0.004654 -0.010688 0.009453
> 0.002676  0.001334  -0.011326
> 7470 Levels: -0.000078 -0.000085 -0.000086 -0.0001 -0.000112 -0.000115
> -0.000152 -0.000154 -0.000157 -0.00016 -0.000171 -0.000185 -0.000212
> -0.000238 -0.000256 -0.000259 -0.000263 -0.000273 ... C

Hi.

It seems that the first file contains a non-numeric row. It may contain "C",
which is the last of the levels. In this case, the whole column is
considered as a character vector and is converted to a factor.

> > returns2[1:10,1]
>  [1] -0.002206  0.115696 -0.015192  0.008719 -0.004654 -0.010688  0.009453
> 0.002676  0.001334 -0.011326
> 
> > as.numeric(returns[2528:2537,1])
>  [1]  341 7444 2244 5149  787 1717 5251 4122 3878 1811

These are indices to the levels of the factor.

Petr Savicky.



More information about the R-help mailing list