Re: [R] Question about levels/as.numeric

From: Petr Savicky <savicky_at_praha1.ff.cuni.cz>
Date: Sun, 10 Apr 2011 23:22:29 +0200

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.



R-help_at_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. Received on Sun 10 Apr 2011 - 21:25:59 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 10 Apr 2011 - 21:50:29 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive