[R] ?bug? strange factors produced by chron

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Feb 13 11:54:21 CET 2006


1) The obvious test is via is.factor(), and you have not used that.

2) Your example works for me, so what versions of R and chron is this?

3) Here's my guess. split is using the C-level test isFactor.  That tests 
that the factor is of type integer, so please try

>  typeof(kvartaly)

I suspect you will get "double" and not "integer", and if so you can fix 
this by

storage.mode(kvartaly) <- "integer"

So here's an example which will fail

> fac2 <- rep(c(1,2,3), each=5)
> attr(fac2, "levels") <- as.character(1:3)
> oldClass(fac2) <- "factor"
> is.factor(fac2)
[1] TRUE
> split(rnorm(15), fac2)
Error in split(x, f) : second argument must be a factor

I think it is an error that the R-level and C-level tests for is.factor() 
are different.


On Mon, 13 Feb 2006, Petr Pikal wrote:

> Hallo all
>
> Please help me. I am lost and do not know what is the problem. I have
> a factor called kvartaly.
>
>> attributes(kvartaly)
> $levels
> [1] "1Q.04" "2Q.04" "3Q.04" "4Q.04" "1Q.05" "2Q.05" "3Q.05" "4Q.05"
> $class
> [1] "factor"
>> mode(kvartaly)
> [1] "numeric"
>> str(kvartaly)
> Factor w/ 8 levels "1Q.04","2Q.04",..: 1 1 1 1 1 1 1 1 1 1 ...
>>
>
> but if I call split it throws an error
>
>> split(rnorm(731),kvartaly)
> Error in split(x, f) : second argument must be a factor
>
> so I tried to make a test example which works if I try to construct
> factor manually but fails if I use chron
>
> vec<-c("1Q.04", "1Q.05", "1Q.06")
> fac<-as.factor(rep(vec,c(5,5,5)))
>
> split(rnorm(15),fac)
> $"1Q.04"
> [1]  1.9803999 -0.3672215 -1.0441346  0.5697196 -0.1350546
>
> $"1Q.05"
> [1]  2.40161776 -0.03924000  0.68973936  0.02800216 -0.74327321
>
> $"1Q.06"
> [1]  0.1887923 -1.8049586  1.4655549  0.1532533  2.1726117
>
> vec1<-as.Date(Sys.time())

Why not Sys.Date() ?

> vec1<-c(vec1, vec1-100, vec1-300)
> vec1<-rep(vec1,c(5,5,5))
> fac1<-interaction(quarters(as.chron(as.POSIXct(vec1))),
> format(vec1,"%y"))
>> split(rnorm(15),fac1)
> Error in split(x, f) : second argument must be a factor
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>
> Why split does not accept fac1 if according to all tests it **is** a
> factor?
>
> Thank you
> Petr
>
> Petr Pikal
> petr.pikal at precheza.cz

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list