We can do so using the := notation. This is a notation that is already available in R, but generally not used. It allows the update to happen in-place. In other words, it avoids making a copy of the dataset in order to add a new column, as shown:
dstate[,Region:=state.region] dstate[1:3] # We can see that the Region column has been added
We can use := to add multiple columns.
For instance, to add the division and abbrevation of each state, we can use the following:
dstate[,c("Division","Abb"):=.(state.division, state.abb)] dstate[1:3] # We can see that the new columns, Division and Abb have been added
To find the sum of Population grouped by Region, we can use the following:
dstate[,.(Sum_Pop=sum(Population)),by=Region] ...