R

Asked • 09/09/19

How to reorder data.table columns (without copying)?

I'd like to reorder columns in my `data.table` `x`, given a character vector of column names, `neworder`: library(data.table) x <- data.table(a = 1:3, b = 3:1, c = runif(3)) neworder <- c("c", "b", "a")Obviously I could do: x[ , neworder, with = FALSE] # or x[ , ..neworder] # c b a # 1: 0.8476623 3 1 # 2: 0.4787768 2 2 # 3: 0.3570803 1 3but that would require copying the entire dataset again. Is there another way to do this?

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.