By using the R base methods and add_column() function from tidyverse package we can add an empty column or a variable to the DataFrame. In this article, I will explain how to add an empty column in R, add multiple empty columns to the DataFrame,
1. Quick Examples of Add Empty Column to DataFrame
The following are quick examples of how to add an empty column to the DataFrame.
# Quick Examples
# Add empty column
df$empty_column = NA
# By using bracket notation
df['empty_column'] <- NA
# Add empty column
df2 <- cbind(df, empty_column=NA)
# Add multiple empty columns
df2 <- cbind(df, empty_column1=NA,empty_column2=NA)
library('tidyverse')
# using add_column()
df2 <- df %>%
add_column(empty_column = NA)
# Add multiple empty columns
df2 <- df %>%
add_column(col1_name = NA,
col2_name = NA)
Let’s create an R DataFrame, run these examples and explore the output. If you already have data in CSV you can easily import CSV files to R DataFrame. Also, refer to Import Excel File into R.
# Create dataframe
df = data.frame(id=c(11,22),
pages=c(32,45),
name=c("spark","python"))
df
# Output
# id pages name
#1 11 32 spark
#2 22 45 python
2. Add Empty Column to DataFrame
Use $ operator, square bracket [] notation, and cbin() function to add an empty column to DataFrame. besides this, you can also use the add_column() function from tidyverse package.
2.1 Using R Base $ Operator to Add Empty Column
R base uses the $ operator to refer to a column of the DataFrame, by using this operator let’s add an empty column to the DataFrame in R. The following example adds a new column with an empty value NA. In R, NA is considered an empty or missing value.
# Using $ notation
df$empt_column <- NA
df
# Output
# id pages name empty_column
#1 11 32 spark NA
#2 22 45 python NA
2.2 Using R base [] Notation
You can also use df['column']
add an empty column to R DataFrame, This gives the same result as the $ operator however, this is the best approach to use as it is more user-friendly and the code is easy to read.
# By using bracket notation
df['empt_column'] <- NA
df
Yields the same output as above.
2.3 Using R base cbin() Function
To add an empty column in R, use cbin()
function. This function takes a DataFrame as a first argument and an empty column you wanted to add as a second argument. Note that this doesn’t update the existing DataFrame instead it returns a copy of the DataFrame after adding an empty column.
# Add empty column to DataFrame
df2 <- cbind(df, empty_column=NA)
df2
Yields the same output as above.
2.2 Add Empty Column using tidyverse package
add_column()
function from tidyverse can also be used to add an empty column to DataFrame in R. In order to use this function first, you need to install R package by using install.packages("tidyverse")
and load it using the library("tidyverse")
.
# Load library
library('tidyverse')
# Add empty column
df2 <- df %>%
add_column(empty_column = NA)
df2
3. Add Multiple Empty Columns to DataFrame
Sometimes you may be required to add multiple empty columns, you can achieve this by using R base functions and tidyverse functions.
3.1 Use cbin()
In the above section, you have learned how to add a single empty column, by using the same R cbin()
function you can add multiple empty columns to DataFrame (data.frame). The following example adds empty_column1
and empty_columns2
to the DataFrame with the value NA to all rows.
# Add multiple empty columns
df2 <- cbind(df, empty_column1=NA,empty_column2=NA)
df2
# Output
# id pages name empty_column1 empty_column2
#1 11 32 spark NA NA
#2 22 45 python NA NA
3.2 Using Bracket Notation
The following example uses square bracket notation and adds multiple columns with NA values.
# Define empty columns
empty_cols <- c('col1', 'col2', 'col3')
# Add multiple empty columns
df[ , empty_cols] <- NA
3.3 Use add_column() to add Multiple Columns
We can also use add_column() from tidyverse package to add multiple columns. The following example adds empty columns col1_name
and col2_name
.
# Add multiple empty columns
df2 <- df %>%
add_column(col1_name = NA,
col2_name = NA)
df2
# Output
# id pages name col1_name col2_name
#1 11 32 spark NA NA
#2 22 45 python NA NA
4. Conclusion
In this article, you have learned how to add an empty column to the DataFrame (data.frame) in R by using the R base function and add_columns() of tidyverse package.
Related Articles
- How to Add Row to DataFrame in R?
- How to Add Element to List in R?
- How to Add Element to Vector in R?
- How to create an Empty DataFrame in R?
- How to create an Empty DataFrame with column names in R?
- How to Replace Empty String with NA in R?
- How to Replace NA with Empty String in R?