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 the table is keyed 
# [1] "Region" 
 
dstate[.("West")] # View all entries with Region "West" 
 

We can also set multiple keys. For instance, to set both Region and Division as key we use the following code:

setkey(dstate,Region,Division) 

key(dstate) 
dstate[.("West","Mountain")] # View all entries with Region "West" and Division "Mountain"  

Notice that the keys are being passed as the i value in the corresponding data.table notation as we are subsetting the table. We can use this in conjunction with the j value to return only specific columns, as shown:

dstate[.("West","Mountain"),.(Mean=mean(Area))] # Find the Mean Area for Region "West" and Division "Mountain" 
 
dstate[.("West"),.(Mean=mean(Area)), by = Division] # Find the Mean Area for Region "West" group-ed by Division 
 
# A couple of addition arguments, mult and nomatch allows the user to specify the behaviour if multiple matches or no matches are found 
 
dstate[.("West","Mountain"),mult="first"] 
 
dstate[.("West",c("Mountain","InvalidEntry")),nomatch=0L] # Skips no matches  

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.3.167