Add Column to DataFrame in R

To add a new column or variable to the DataFrame (data.frame) in R use either $ operator, [] notation, cbin() base function or add_column() function from tibble package.

In this article, I will explain how to add a new column in R, add multiple columns, add a column with a constant value, insert before or after the column e.t.c

The following are different methods to add new columns to DataFrame in R that I covered in this article.

  1. $ operator
  2. [] notation
  3. cbin() R base function
  4. add_column() function from tibble.
  5. mutate() function from dplyr.

1. Quick Examples of Add Column to DataFrame

Following are quick examples of how to add a single column or multiple columns to DataFrame in R.


# Below are quick examples

# Using $ notation
chapters = c(76,86)
df$chapters = chapters

# By using bracket notation
chapters = c(76,86)
df['chapters'] <- chapters

# Add new column
chapters = c(76,86)
df2 <- cbind(df, chapters)

# Add multiple columns to dataframe
chapters = c(76,86)
price=c(144,553)
df3 <- cbind(df, chapters, price)

# Add new column from existing column
df$new_pages <- df$pages-2

library('tidyverse')
# using add_column()
df2 <- df %>%
  add_column(add_column = "constant_value")

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. Using $ Operator

By using $ donation to refer to the column name of DataFrame can be used to add a new column to DataFrame in R. In order to add first create a new column using vector and assign this to the df$new_colname. The following example adds a new column chapters to the DataFrame.


# Using $ notation
chapters = c(76,86)
df$chapters <- chapters
df

# Output
#  id pages   name chapters
#1 11    32  spark       76
#2 22    45 python       86

3. Using [] Notation

Similarly, another way to refer to a column name is by using df['new_column'], assigning a vector to this to have a new column. 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
chapters = c(76,86)
df['chapters'] <- chapters
df

Yields the same output as above.

4. Using cbin() Function

To add a new column in R, use cbin() function. This function takes a DataFrame as a first argument and for the second argument, use the vector which creates a new column on the DataFrame. Note that this doesn’t update the existing DataFrame instead it returns a copy of the DataFrame after adding a new column.


# Add new column
chapters = c(76,86)
df2 <- cbind(df, chapters)
df2

Yields the same output as above.

5. Add Multiple Columns to DataFrame

By using the same cbin() function you can add multiple columns to the DataFrame in R. The following example adds columns chapters and price to the DataFrame (data.frame).


# Add multiple columns to dataframe
chapters = c(76,86)
price=c(144,553)
df3 <- cbind(df, chapters, price)

# Output
#  id pages   name chapters price
#1 11    32  spark       76   144
#2 22    45 python       86   553

6. Add a New Column from Existing

Finally using the R base let’s see how to add a new column from the existing DataFrame column.


# Add new column from existing column
df$new_pages <- df$pages-2
df

# Output
#  id pages   name new_pages
#1 11    32  spark        30
#2 22    45 python        43

7. By using add_column() from tidyverse

Use add_column() function from tidyverse to add a column to the DataFrame in R. This is a convenient way to add one or more columns to an existing data frame. 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").

The following example adds a constant column to DataFrame. All rows will have the same value.


# Load tidyverse
library("tidyverse")

# Using add_column()
df2 <- df %>%
  add_column(add_column = "constant_value")
df2

# Output
#  id pages   name     add_column
#1 11    32  spark constant_value
#2 22    45 python constant_value

By using the methods explained above you can also add empty column to DataFrame in R.

Conclusion

In this article, you have learned how to add a new column in R, add multiple columns, add a column with a constant value, e.t.c

Related Articles

References

r add column

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply

You are currently viewing Add Column to DataFrame in R