Data.table not accepting "by" and "format" (by date) at the same time

I use data.tableto find the average “session” date, but I am having trouble formatting it the way I want, and I'm confused that the problem is:

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 each session to have a label based on when that session was. I decided to designate each session as the month during which the session took place (formatted as "% b-% Y"), but since the sessions sometimes intersect after 2 months, I want to do this by taking the average date of this session and using this to determine the label .

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

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

I can also reformat the average date, as I want, to data.table:

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

:

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

:

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

? , , ?

. , , (), , , . - , , :

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

, mean.Date mean:

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

, format.Date

+3

Source: https://habr.com/ru/post/1657569/


All Articles