Given a data.table
with an arbitrary number of columns
dt = data.table( a = letters[1:5], b = rep('-', 5), c = LETTERS[1:5] )
# a b c
# 1: a - A
# 2: b - B
# 3: c - C
# 4: d - D
# 5: e - E
and an arbitrary format
string having a number of placeholders corresponding to the number of columns
format = '%s0%s1%s'
How to apply sprintf
without explicitly calling it with all column names?
Just provide the data.table
won't work because sprintf
expects 3 arguments in this case. Calling sprintf(format, dt$a, dt$b, dt$b)
is no option because I don't know neither the format
nor the data.table
beforehand.
Also sapply
on row indices won't work because the operation has to be done on a subset of rows preserving their order.
idx = seq( 1, by = 2, to = 5 )
So the goal is by issueing a hypothetical command
dt[ idx, sprintf( format, * )]
achieving this
# [1] "a0-1A" "c0-1C" "e0-1E"
It can be done by calling
cols = paste( names( dt ) ), collapse=',' )
# "a,b,c"
eval( parse( text = sprintf( 'dt[ idx, sprintf( format,%s )]', cols ) ) )
# [1] "a0-1A" "c0-1C" "e0-1E"
But this is rather arcane and definetly not clean code and it would be nice if there was a more semantic way in the data.table
package itself.
So the question is basically if there is one. Until now neither google nor the the data.table
manual gave me an answer.
Furthermore I do neither want to paste
some/multiple columns but I want to use all columns instead nor I am looking for a way to achieve this by the use of data.frame
. I am looking for smooth data.table
syntax.