Add Multiple New Columns to data.table in R - GeeksforGeeks

16 Jul, 2021
In this article, we will discuss how to Add Multiple New Columns to the data.table in R Programming Language. To do this we will first install the data.table library and then load that library.
Syntax:
install.packages(“data.table”)
After installing the required packages out next step is to create the table. We create a table with the help of a “data.table” and store that table in a variable. Later if the requirement persists a new column can be added by first creating a column as list and then adding it to the existing data.table by one of the following methods.
A column can be added to an existing data table using := operator.
Syntax:
‘:='(data type, constructors)
Here ‘:’ represents the fixed values and ‘=’ represents the assignment of values. So, they together represent the assignment of fixed values. Therefore, with the help of “:=” we will add 2 columns in the above table. So, to do this first we will create the columns and try to put data in it, we will do this by creating a vector and put data in it.
Given below are various examples to support this.
Example:
R
# Create table using data.tabledata <- data.table(x1 = 1:6,                      x2 = 3:8)                              y1 <- 10:15                          y2 <- 4:9 # := is used to add columns in# our tabledata[ , `:=` (y1 = y1, y2 = y2)] # print the datadata
Output:
fig 3: Table after adding columns
We can also add the column in the table using the data that already exist in the table. While adding the data with the help of colon-equal symbol we define the name of the column i.e. z1 and z2 then during adding data we multiply the ‘x1’ and ‘x2’ in the z1 column, and we multiply the ‘y1’ and ‘y2’ in the z2 column and at last, we print the table.
Example:
R
# create the tabledata <- data.table(x1 = 1:6,                         x2 = 3:8) # create the columns which we# are going to be add in tabley1 <- 10:15y2 <- 4:9 # add the columns with the help# of ":=" into the tabledata[, ':='(y1=y1,y2=y2)] # create the data in column z1# and z2 by multiplying x1,x2 for z1# and by multiplying y1 and y2# for z2 and add it to table by using ":="data[, ':='(z1=x1*x2,z2=y1*y2)] # print the tabledata
Output:
fig 6: Table after adding z1 and z2
In this method, we use the dot “.” with the “by”. Here “.” is used to put the data in the new columns and by is used to add those columns to the data table. So, they together are used to add columns to the table. 
Example:
R
# creating the tabledata <- data.table(x1 = 1:6,                    x2 = 3:8) # creating the data of other# columnsy1 <- 10:15y2 <- 4:9 # add the above columns into# the tabledata[, ':='(y1=y1,y2=y2)] # create extra column with the# help of data in the tabledata[, ':='(z1=x1*x2,z2=y1*y2)] # create new columns by adding the# column data of existing data# add those created column to the# table with the help of by functiondata[, .(a1=x1+y1+z1,a2=x2+y2+z2),     by=.(x1,x2,y1,y2,z1,z2)]
Output:
fig 7: Table after adding a1, a2
sweetyty
Picked
R DataTable
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Change Color of Bars in Barchart using ggplot2 in R
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
How to import an Excel File into R ?
How to filter R DataFrame by values in a column?
How to filter R dataframe by multiple conditions?
How to change the order of bars in bar chart in R ?
R - if statement
Replace Specific Characters in String in R