[R] printing POSIXct values in table labels

ripley@stats.ox.ac.uk ripley at stats.ox.ac.uk
Thu Mar 6 09:25:56 CET 2003


table() turns its argument into a factor, as it is documented to work on 
factors, only, and

> factor(x$date)
[1] 1034809200 1034809200 1034809200 1034809200 944611200  944611200 
Levels: 944611200 1034809200

That's because unique.default does not know about POSIXct objects (nor 
indeed many other classes).  It had to be unique.default to avoid the 
matrix and data frame methods.

I suggest you coerce to a suitable factor yourself for now.


On Wed, 5 Mar 2003, David Kane  <David Kane wrote:

> Hi,
> 
> I think that there is something that I am misunderstanding in creating tables
> using dates that are of class POSIXct. Consider:
> 
> > x <- data.frame(date = as.POSIXct(strptime(c(rep("2002-10-17", 4), rep("1999-12-08", 2)), format = "%Y-%m-%d")))
> > x
>         date
> 1 2002-10-17
> 2 2002-10-17
> 3 2002-10-17
> 4 2002-10-17
> 5 1999-12-08
> 6 1999-12-08
> > table(x$date)
> 
>  944629200 1034827200 
>          2          4 
> 
> I understand that the headings here are the number of seconds since some
> epoch. But shouldn't the values print out here as they print out in the
> dataframe?
> 
> I can get the effect that I want using:
> 
> > table(format(x$date))
> 
> 1999-12-08 2002-10-17 
>          2          4 
> 
> The problem is that this can add a lot of time to the calculation in big
> dataframes.
> 
> > x <- data.frame(date = as.POSIXct(strptime(c(rep("2002-10-17", 40000), rep("1999-12-08", 20000)), format = "%Y-%m-%d")))
> > dim(x)
> [1] 60000     1
> > system.time(table(x$date))
> > [1] 0.19 0.00 0.19 0.00 0.00
> > system.time(table(format(x$date)))
> [1] 18  0 19  0  0
> 
> Am I missing something?
> 
> > R.version
>          _                   
> platform sparc-sun-solaris2.6
> arch     sparc               
> os       solaris2.6          
> system   sparc, solaris2.6   
> status                       
> major    1                   
> minor    6.2                 
> year     2003                
> month    01                  
> day      10                  
> language R                   
> > 
> 
> Thanks,
> 
> Dave Kane
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> 

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