How to Add an Empty Column to DataFrame in R?
In this article, we will discuss how to add an empty column to the dataframe in R Programming Language.
Add One Empty Column to dataframe
Here we are going to add an empty column to the dataframe by assigning column values as NA.
Syntax:
dataframe[ , 'column_name'] = NA
where,
- dataframe is the input dataframe
- column_name is the new column name
Example: R program to create a dataframe with 3 columns and add an empty column
R
data = data.frame (marks1= c ( '90' , '78' , '89' , '76' ),
marks2= c ( '92' , '68' , '78' , '96' ),
marks3= c ( '90' , '78' , '89' , '76' ))
data[, 'empty_column' ] = NA
print (data)
|
Output:
Add Multiple Empty Columns to dataframe
We can add multiple columns with empty values in dataframe by passing column names in c() function
Syntax:
data[,c('column_name1',column_name2',............,'column_name n')]
Example: R program to add 4 empty columns
R
data = data.frame (marks1= c ( '90' , '78' , '89' , '76' ),
marks2= c ( '92' , '68' , '78' , '96' ),
marks3= c ( '90' , '78' , '89' , '76' ))
data[, c ( 'empty1' , 'empty2' , 'empty3' , 'empty4' )] = NA
print (data)
|
Output:
Last Updated :
15 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...