Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of "everything()" operator in "data.table"? [duplicate]

Tags:

r

data.table

Let see the example:

 # 1. dplyr
 mtcars %>% select(mpg, cyl, gear, everything())

 # 2. data.table
 as.data.table(mtcars)[, .(mpg, cyl, gear)]

What should be added after "gear" to have the same output as in "dplyr" case?

Thanks

like image 562
Andrii Avatar asked May 08 '19 09:05

Andrii


People also ask

What is := in data table?

table way. Unlike data. frame, the := operator adds a column to both the object living in the global environment and used in the function. I think this is because these objects are actually the same object.

What does data table function do in R?

data. table is a package is used for working with tabular data in R. It provides the efficient data. table object which is a much improved version of the default data.

What is data table in R?

data.table is an R package that provides an enhanced version of data.frame s, which are the standard data structure for storing data in base R. In the Data section above, we already created a data.table using fread() . We can also create one using the data.table() function.


1 Answers

Using setcolorder:

setcolorder(mtcars, neworder = c("mpg", "cyl", "gear"))

neworder
Character vector of the new column name ordering. May also be column numbers. If length(neworder) < length(x), the specified columns are moved in order to the "front" of x. By default, setcolorder without a specified neworder moves the key columns in order to the "front" of x.

like image 177
zx8754 Avatar answered Sep 28 '22 03:09

zx8754