?bug? strange factors produced by chron

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

?bug? strange factors produced by chron

PIKAL Petr
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())
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
[hidden email]

______________________________________________
[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
Reply | Threaded
Open this post in threaded view
|

Re: ?bug? strange factors produced by chron

Prof Brian Ripley
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
> [hidden email]

--
Brian D. Ripley,                  [hidden email]
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

______________________________________________
[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
Reply | Threaded
Open this post in threaded view
|

Re: ?bug? strange factors produced by chron

PIKAL Petr
Thank you very much.

On 13 Feb 2006 at 10:54, Prof Brian Ripley wrote:

Date sent:       Mon, 13 Feb 2006 10:54:21 +0000 (GMT)
From:           Prof Brian Ripley <[hidden email]>
To:             Petr Pikal <[hidden email]>
Copies to:       [hidden email]
Subject:         Re: [R] ?bug? strange factors produced by chron

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

I used it with TRUE result but did not use in my post (mea culpa)
> is.factor(kvartaly)
[1] TRUE

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

Package: chron
Version: 2.3-1
R 2.2.1 and W2000

but problem is probably in interaction (see below)
>
> 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)

 > typeof(kvartaly)
 [1] "double"
 
Problem is probably not in chron but in interaction, which silently
transfers factor type to double

> typeof(factor(letters[1:2]))
[1] "integer"

> typeof(interaction(factor(letters[1:2]), factor(letters[3:4])))
[1] "double"
>

>
> I suspect you will get "double" and not "integer", and if so you can
> fix this by
>
> storage.mode(kvartaly) <- "integer"

Thanks, it works.

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

<snip>

> > vec1<-as.Date(Sys.time())
>
> Why not Sys.Date() ?

I remembered only Sys.time when writing my mail.

>

<snip>

Thank you again.

Best regards.
PetrPetr Pikal
[hidden email]

______________________________________________
[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
Reply | Threaded
Open this post in threaded view
|

Re: ?bug? strange factors produced by chron

Prof Brian Ripley
On Mon, 13 Feb 2006, Petr Pikal wrote:

> Thank you very much.
>
> On 13 Feb 2006 at 10:54, Prof Brian Ripley wrote:
>
> Date sent:       Mon, 13 Feb 2006 10:54:21 +0000 (GMT)
> From:           Prof Brian Ripley <[hidden email]>
> To:             Petr Pikal <[hidden email]>
> Copies to:       [hidden email]
> Subject:         Re: [R] ?bug? strange factors produced by chron
>
>> 1) The obvious test is via is.factor(), and you have not used that.
>
> I used it with TRUE result but did not use in my post (mea culpa)
>> is.factor(kvartaly)
> [1] TRUE
>
>> 2) Your example works for me, so what versions of R and chron is this?
>
> Package: chron
> Version: 2.3-1
> R 2.2.1 and W2000
>
> but problem is probably in interaction (see below)
>>
>> 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)
>
> > typeof(kvartaly)
> [1] "double"
>
> Problem is probably not in chron but in interaction, which silently
> transfers factor type to double
>
>> typeof(factor(letters[1:2]))
> [1] "integer"
>
>> typeof(interaction(factor(letters[1:2]), factor(letters[3:4])))
> [1] "double"

2.2.1 does not do that for me (and contains ans <- as.integer(ans), and
has since Sept 2001).  Do you have a private copy?

--
Brian D. Ripley,                  [hidden email]
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

______________________________________________
[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
Reply | Threaded
Open this post in threaded view
|

Re: ?bug? strange factors produced by chron

PIKAL Petr
Hi

On 13 Feb 2006 at 12:04, Prof Brian Ripley wrote:

Date sent:       Mon, 13 Feb 2006 12:04:49 +0000 (GMT)
From:           Prof Brian Ripley <[hidden email]>
To:             Petr Pikal <[hidden email]>
Copies to:       [hidden email]
Subject:         Re: [R] ?bug? strange factors produced by chron

> On Mon, 13 Feb 2006, Petr Pikal wrote:
>
> > Thank you very much.
> >
> > On 13 Feb 2006 at 10:54, Prof Brian Ripley wrote:
> >
> > Date sent:       Mon, 13 Feb 2006 10:54:21 +0000 (GMT)
> > From:           Prof Brian Ripley <[hidden email]>
> > To:             Petr Pikal <[hidden email]>
> > Copies to:       [hidden email]
> > Subject:         Re: [R] ?bug? strange factors produced by chron
> >
> >> 1) The obvious test is via is.factor(), and you have not used that.
> >
> > I used it with TRUE result but did not use in my post (mea culpa)
> >> is.factor(kvartaly)
> > [1] TRUE
> >
> >> 2) Your example works for me, so what versions of R and chron is
> >> this?
> >
> > Package: chron
> > Version: 2.3-1
> > R 2.2.1 and W2000
> >
> > but problem is probably in interaction (see below)
> >>
> >> 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)
> >
> > > typeof(kvartaly)
> > [1] "double"
> >
> > Problem is probably not in chron but in interaction, which silently
> > transfers factor type to double
> >
> >> typeof(factor(letters[1:2]))
> > [1] "integer"
> >
> >> typeof(interaction(factor(letters[1:2]), factor(letters[3:4])))
> > [1] "double"
>
> 2.2.1 does not do that for me (and contains ans <- as.integer(ans),
> and has since Sept 2001).  Do you have a private copy?

Yes, you have got it. I used (probably due to different handling of
drop) a copy of interaction from Hmisc package from 2001, where there
is no as.integer(ans). I'll switch to base version and see if there
is no other problems with some of my code.

As I noticed this behaviour only recently and use interaction from
time to time so some other change had to sneaked around me without
noticing.

Thank you again.

Best regards.
Petr

>
> --
> Brian D. Ripley,                  [hidden email]
> 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
>
> ______________________________________________
> [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

Petr Pikal
[hidden email]

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