Creating new columns in data.table

New columns can be created in data.table using the := notation, as shown:

dstate[,IncomeGreaterThan5000:=Income>5000] # We will add a column IncomeGreaterThan5000 in-place using := 
 
# Adding multiple columns 
 
dstate[,c("IncomeGreaterThan5000","AreaLessThan5000"):=list(Income>5000,Area<5000)] # We will add a column IncomeGreaterThan5000 in-place using := 
 
# alternatively, we can also use `:=` 
 
dstate[,`:=`(IncomeGreaterThan5000=Income>5000,AreaLessThan5000=Area<5000)] # We will add a column IncomeGreaterThan5000 in-place using :=  

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.