Built with Tutify.io

    AIStudyLabs

    How to Add Empty Column to DataFrame in R?

    How to Add Empty Column to DataFrame in R?

    You can use R base methods and the add_column() function from the tidyverse package to add an empty column or a variable to a data frame. In this article, I will explore using various ways of base R and the tidyverse package how we can add an empty column of single or multiple to a data frame.

    <a id="h-1-quick-examples-of-add-empty-column-to-dataframe"></a>

    1. Quick Examples of Add Empty Column to Dataframe

    Below are quick examples of how to add an empty column to the data frame.

    
    # 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 data frame, run these examples, and examine the result.

    
    # 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
    

    <a id="h-2-add-empty-column-to-dataframe"></a>

    2. Add Empty Column to DataFrame

    Use the $ operator, square bracket [] notation, and cbin() function to add an empty column to the data frame. Besides this, you can also use the add_column() function from the tidyverse package.

    <a id="h-2-1-using-r-base-operator-to-add-empty-column"></a>

    2.1 Using R Base $ Operator to Add Empty Column

    R base uses the $ operator to refer to a column of the data frame, by using this operator let's add an empty column to the data frame 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
    

    <a id="h-2-2-using-r-base-notation"></a>

    2.2 Using R base [] Notation

    You can add an empty column to an R data frame using df['column']. This process produces the same result as using the $ operator, it is considered the best approach because it is more user-friendly and makes the code easier to read.

    
    # By using bracket notation
    df['empt_column'] <- NA
    df
    

    Yields the same output as above.

    <a id="h-2-3-using-r-base-cbin"></a>

    2.3 Using R base cbin()

    To add an empty column in R, use the cbind() function. This function accepts a data frame as the first argument and the empty column to be added as the second argument. Note that this operation does not alter the original data frame instead, it returns a new data frame with the empty column included.

    
    # Add empty column to DataFrame
    df2 <- cbind(df, empty_column=NA)
    df2
    

    Yields the same output as above.

    <a id="h-2-4-add-empty-column-using-tidyverse-package"></a>

    2.4 Add Empty Column using tidyverse package

    add_column() function from tidyverse can also be used to add an empty column to the data frame in R. To use this function, you first need to install the R package by running install.packages("tidyverse"), and then load it with library("tidyverse")

    
    # Load library
    library('tidyverse')
    
    # Add empty column
    df2 <- df %>%
      add_column(empty_column = NA)
    df2
    

    <a id="h-3-add-multiple-empty-columns-to-dataframe"></a>

    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.

    <a id="h-3-1-use-cbin"></a>

    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 the data frame (data.frame). The following example adds empty_column1 and empty_columns2 to the data frame 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
    

    <a id="h-3-2-using-bracket-notation"></a>

    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
    

    <a id="h-3-3-use-add-column-to-add-multiple-columns"></a>

    3.3 Use add_column() to add Multiple Columns

    We can also use add_column() from the 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
    

    <a id="h-4-conclusion"></a>

    4. Conclusion

    In this article, you have learned the concept of adding an empty column to the data frame (data.frame) in R by using the R base function and add_columns() of tidyverse package.

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References