Suppose I want to have a data.table that contains user supplied columns and programmer specified renaming, for example, the following code:
iris <- data.table::data.table(iris)
# details varies, users are smart and don't specify columns that will be renamed.
details <- c('Sepal.Length', 'Sepal.Width')
cbind(iris[, .(Type = Species)], iris[, details, with = FALSE])
I'm interested in the most efficient and elegant method to select and rename the columns. What is it? We haven't been able to do better than the cbind() above. data.table is of a reasonable size, say 500.000 rows by 8 columns.
Addendum
I've ran the solutions provided by Akrun with microbenchmark, they perform quite well:
> microbenchmark(
cbind1 = cbind(iris3000[, .(Type = Species)], iris3000[, details, with = FALSE]),
cbind2 = cbind(iris3000[, .(Type = Species)], iris3000[, ..details]),
assignment = iris3000[, ..details][, Type := iris3000$Species][],
setnames1 = setnames(iris3000[, .SD, .SDcols = c("Species", details)], "Species", "Type")[],
setnames2 = setnames(iris3000[, c("Species", ..details)], 1, "Type")[],
times = 500L
)
Unit: milliseconds
expr min lq mean median uq max neval
cbind1 4.900362 5.304078 6.306950 5.414371 5.558222 21.77936 500
cbind2 4.910121 5.296428 6.720642 5.411720 5.514940 221.38992 500
assignment 2.108325 2.284778 2.738970 2.333464 2.394961 18.55119 500
setnames1 2.155007 2.372021 2.857554 2.423696 2.483803 20.26001 500
setnames2 1.832926 2.048634 2.960401 2.095429 2.145081 204.90523 500
The case details <- NULL also works and is faster.