Skip to content

combining mean with reformatted Date class and by throwing an error #1876

@rossholmberg

Description

@rossholmberg

I've come across an issue using a formatted date column with by. Take the following data:

library( data.table )
data <- data.table( session = c( 1,1,1,1,2,2,2,2,2,2,3,3,3,3 ),
                    date = as.Date( c( "2016-01-01", "2016-01-02", "2016-01-03", "2016-01-03",
                                       "2016-04-30", "2016-04-30", "2016-05-03", "2016-05-03", "2016-05-03", "2016-05-03",
                                       "2016-08-28", "2016-08-28", "2016-08-28", "2016-08-28" ) )
)

I want to mark each session with a label, formatted %b-%Y, based on the mean date for that session.

I can find the mean date of each session, using the by parameter:

output <- copy( data )[ , Month := mean( date ), by = session ]

I can also reformat a mean date the way I want within data.table:

output <- copy( data )[ , Month := format( mean( date ), "%b-%Y" ) ]

But I can't do both:

output <- copy( data )[ , Month := format( mean( date ), "%b-%Y" ), by = session ]

The above returns an error:
Error in prettyNum(.Internal(format(x, trim, digits, nsmall, width, 3L, : invalid 'trim' argument In addition: Warning message: In mean(date) : argument is not numeric or logical: returning NA

Note I can do what I need in two steps (below), and it works OK, but there does seem to be something going wrong with the above :

output <- copy( data )[ , Month := mean( date ), by = session 
                        ][ , Month := format( Month, "%b-%Y" ) ]

It also works fine with mean.Date:

output <- copy( data )[ , Month := format( mean.Date( date ), "%b-%Y" ), by = session ]

Metadata

Metadata

Assignees

No one assigned

    Labels

    GForceissues relating to optimized grouping calculations (GForce)

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions