In R, you can use the following methods to add multiple columns to a data frame in R.
Method 1: Add Multiple Columns to data.frame Object
df[c('new_col1', 'new_col2', 'new_col3',..)] <- NA
Method 2: Add Multiple Column to data.table object
library(data.table)
df[ , ':='(new_col1 = new_col1, new_col2 = new_col2, new_col3 = new_col3,...)]
The following examples shows how to add multiple columns to data frame.
How to Add Multiple Columns to data.frame Object
Suppose you have data frame already created and you want to add multiple columns to it.
Let’s start by creating data frame first:
# Create data frame
df <- data.frame(Machine_name=c("A","D","B","C","B","D","C","A"),
Pressure1=c(78.2, 81.21, 71.7, 80.21, 71.7, 81.21, 80.21, 78.2),
Temperature1=c(31, 33, 36, 37, 36, 33, 37, 31),
Status=c(TRUE,TRUE,FALSE,TRUE,FALSE,TRUE,TRUE,TRUE))
# Show data frame
print(df)
Output:
Machine_name Pressure1 Temperature1 Status
1 A 78.20 31 TRUE
2 D 81.21 33 TRUE
3 B 71.70 36 FALSE
4 C 80.21 37 TRUE
5 B 71.70 36 FALSE
6 D 81.21 33 TRUE
7 C 80.21 37 TRUE
8 A 78.20 31 TRUE
The output shows data frame that we created using data.frame() function.
Now add multiple columns to data frame using syntax given above:
# Add new columns to data frame
df[c('Pressure2', 'Pressure3', 'Temperature2')] <- NA
# Print data frame
print(df)
Output:
Machine_name Pressure1 Temperature1 Status Pressure2 Pressure3 Temperature2
1 A 78.20 31 TRUE NA NA NA
2 D 81.21 33 TRUE NA NA NA
3 B 71.70 36 FALSE NA NA NA
4 C 80.21 37 TRUE NA NA NA
5 B 71.70 36 FALSE NA NA NA
6 D 81.21 33 TRUE NA NA NA
7 C 80.21 37 TRUE NA NA NA
8 A 78.20 31 TRUE NA NA NA
Here the output shows new three columns with NA values added to data frame.
How to Add Multiple Columns to data.table Object
The other method to add multiple columns to data frame is by adding column to data.table Object.
Let’s first create data table:
# Import library
library(data.table)
# Create data table
dt <- data.table(Machine_name=c("A","D","B","C","B","D","C","A"),
Pressure1=c(78.2, 81.21, 71.7, 80.21, 71.7, 81.21, 80.21, 78.2),
Temperature1=c(31, 33, 36, 37, 36, 33, 37, 31),
Status=c(TRUE,TRUE,FALSE,TRUE,FALSE,TRUE,TRUE,TRUE))
# Print data table
print(dt)
Output:
Machine_name Pressure1 Temperature1 Status
<char> <num> <num> <lgcl>
1: A 78.20 31 TRUE
2: D 81.21 33 TRUE
3: B 71.70 36 FALSE
4: C 80.21 37 TRUE
5: B 71.70 36 FALSE
6: D 81.21 33 TRUE
7: C 80.21 37 TRUE
8: A 78.20 31 TRUE
The output shows data table which created using data.table() function from data.table library.
Now add columns to data table by following syntax:
# Declare new columns
Pressure2=c(58.20,56.21,52.47,53.96,52.96,51.29,53.47,58.96)
Temperature2=c(25,26,28,27,21,22,25,22)
# Add new columns
dt[ , ':='(Pressure2 = Pressure2, Temperature2 = Temperature2)]
# Print data table
print(dt)
Output:
Machine_name Pressure1 Temperature1 Status Pressure2 Temperature2
1: A 78.20 31 TRUE 58.20 25
2: D 81.21 33 TRUE 56.21 26
3: B 71.70 36 FALSE 52.47 28
4: C 80.21 37 TRUE 53.96 27
5: B 71.70 36 FALSE 52.96 21
6: D 81.21 33 TRUE 51.29 22
7: C 80.21 37 TRUE 53.47 25
8: A 78.20 31 TRUE 58.96 22
The output shows data table with three new columns.