Ordering columns

We can order the rows by using order() as the j value in data.table as follows:

dstate[order(Region)] 

We can order using multiple columns by just adding them to order() as shown:

dstate[order(Region, State)] # Ascending Region, Ascending State 
dstate[order(Region,-State)] # Ascending Region, Descending State 
 ## Joining tables using data.table 

data.table includes a very useful function called setkey() and setkeyv(). There are other set* functions that, when used along with :=, modify the object by reference. It is also extremely useful when merging tables, as discussed next.

To query the table by Region, we can set Region as key and query by simply using the value of Region:

setkey(dstate,Region) key(dstate) # Check if ...

Get Hands-On Data Science with R now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.