This suppose to be an easy task and trivial, but I couldn't get it work. I would like to create a new table use the same order as another table. Suppose I have current data with same 5 variables from past data but with different order. I would like the current data the same structure. I tried to use the list of colnames from old data and use R data.table to arrange the variable, however as you can see from the example, the new data becases either a list/character or data.table with empty data. Any suggestions? Thanks!
> str(new)
Classes ‘data.table’ and 'data.frame': 2 obs. of 7 variables:
$ id: int 1 2
$ x1: int 0 1
$ x4: int 1 0
$ x5: int 1 0
$ x2: int 0 0
$ x3: int 1 0
$ x6: int 0 1
- attr(*, ".internal.selfref")=<externalptr>
> str(old)
Classes ‘data.table’ and 'data.frame': 2 obs. of 6 variables:
$ id: int 1 2
$ x1: int 1 0
$ x2: int 1 0
$ x3: int 1 0
$ x4: int 0 0
$ x5: int 1 0
- attr(*, ".internal.selfref")=<externalptr>
namelist <- list(names(old))
newlist <- new[,c(namelist)] # using data.table to order the variables
newlist <- new[,unlist(namelist)] # newlist is character instead of data.table
newlist <- new[,c(namelist)] # data.table but not expected data.
class(newlist)
[1] "data.table" "data.frame"
> str(newlist)
Classes ‘data.table’ and 'data.frame': 6 obs. of 1 variable:
$ V1: chr "id" "x1" "x2" "x3" ...
- attr(*, ".internal.selfref")=<externalptr>