Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change column position of data.table

Tags:

r

data.table

I am learning to use the data.table package. One of the things I am trying to do is move the last column ("x") to the first column. Here is how I do it for a data frame:

DF <- cbind(x, DF[1:ncol(DF)]) #Rearrange columns

I read up on setcolorder and tried this, but I get an error

setcolorder(DT, c("x", 1: (length(DT)-1) ) )

Does anyone know a better solution?

like image 964
Wet Feet Avatar asked Oct 27 '13 15:10

Wet Feet


People also ask

How do I rearrange columns in DataTable?

ColReorder adds the ability for the end user to be able to reorder columns in a DataTable through a click and drag operation. This can be useful when presenting data in a table, letting the user move columns that they wish to compare next to each other for easier comparison.

How to change the column position of mysql table without losing data?

You can change the column position of MySQL table without losing data with the help of ALTER TABLE command. The syntax is as follows − To understand the above concept, let us create a table.

How to reorder position of columns in table?

Reorder position of columns in table with cutting and pasting. You can reorder the position by cutting the selected column and then pasting it into your needed position in a table. Please do as follows. 1. Select and right click the column range you need to reorder its position, and then click the Cut option from the

How to change DataTable columns order in C #?

How to change Datatable columns order in c#. am created sql table type order is Qty,Unit,Id but in program DataTable order is Id,Qty,Unit. In code Behind am directly pass DataTable to sql table type so the table order is different. Try to use the DataColumn.SetOrdinal method. For example:

How to change a column name by its position in Excel?

You double click a column name, rename it and press enter. Yet using this method, may cause errors in the long run. For example when the column name in the source data changes. In this post you will learn how to change a column name by its position using the Table.ColumnNames formula.


Video Answer


1 Answers

Option 1

Maybe you can use setdiff:

DT <- data.table(A = 1:2, B = 3:4, X = 5:6)
DT
#    A B X
# 1: 1 3 5
# 2: 2 4 6
setcolorder(DT, c("X", setdiff(names(DT), "X")))
DT
#    X A B
# 1: 5 1 3
# 2: 6 2 4

Option 2

Using a modified version of your approach:

setcolorder(DT, c("X", names(DT)[1:(length(DT)-1)]))

or

setcolorder(DT, c(length(DT), 1:(length(DT)-1)))

Why the error in your approach? You were trying to include both column names and numeric column indices. Use one or the other, but not both.


Option 3

I've written a function called moveme (which for the time being you can find at this Gist or at my blog). You enter a string of "move" commands, separated by semicolons. This lets you shuffle around your columns pretty flexibly:

DT <- data.table(matrix(1:20, ncol = 10, dimnames = list(NULL, LETTERS[1:10])))
DT
#    A B C D  E  F  G  H  I  J
# 1: 1 3 5 7  9 11 13 15 17 19
# 2: 2 4 6 8 10 12 14 16 18 20

setcolorder(DT, moveme(names(DT), "E, F first; B last; H, I before G"))
# DT
#     E  F A C D  H  I  G  J B
# 1:  9 11 1 5 7 15 17 13 19 3
# 2: 10 12 2 6 8 16 18 14 20 4
like image 157
A5C1D2H2I1M1N2O1R2T1 Avatar answered Sep 28 '22 02:09

A5C1D2H2I1M1N2O1R2T1