Home » R Tutorials » How to Add Column to a Data Frame in R

How to Add Column to a Data Frame in R

Using the $ operator or square bracket, we can add a column to a data frame. We can create vector data and add it as a column to a data frame.

Use the add_column function in the tibble package to add a new column or append vector to a data frame.

In this tutorial, we will discuss how to add a column to a data frame in R using the $ operator and square bracket and add_column to append a column to a data frame.

Add Column to a Data Frame in R using $ operator

Using the $ operator to add a new column to an existing data frame.

Let’s consider an example to demonstrate how to append a vector to a data frame.

# Create a data frame
student_info <- data.frame(
  name = c("Tom","Kim","Sam","Julie","Emily","Chris"),
  age = c(20,21,19,20,21,22),
  gender = c('M','F','M','F','F','M')
 
)
# Print the data frame
student_info

In the above R code, we have created an example data frame to add a new column using the $ operator.

Let’s create a new column vector.

marks <- c(72,77,65,80,85,87)

Using the existing data frame, append new vector marks to it using the $ operator.

# Add new column to an existing data frame
student_info$marks <- marks
# Print the data frame

The output of the above R code to append a new column to the data frame in R is:

   name age gender marks
1   Tom  20      M    72
2   Kim  21      F    77
3   Sam  19      M    65
4 Julie  20      F    80
5 Emily  21      F    85
6 Chris  22      M    87

Adding a Column to Data Frame in R using Brackets

Using the square brackets with the data frame object, we can add a new column to a data frame.

Let’s practice with the above student_info data frame for adding a column to a data frame.

# Append a vector to data frame
student_info["marks"] = marks
# Prints the data frame
student_info

In the above R code, we have specified marks as a new column in the data frame variable with a square bracket and assigned vector.

It adds a new column to the data frame and prints the data frame as follows:

  name age gender marks
1   Tom  20      M    72
2   Kim  21      F    77
3   Sam  19      M    65
4 Julie  20      F    80
5 Emily  21      F    85
6 Chris  22      M    87

Append New Column to a Data Frame using tidyverse package

Using the tidyverse package and tibble, it can append a new column to a data frame.

Let’s consider the above student_info data frame to add a new column to an existing data frame using the add_column function.

# import tidyverse library
library(tidyverse)
# use pipe operator and add_column to create new column in data frame
data <- student_info %>% add_column(total_marks = marks)
# print the data frame
data

In the above R code to create a new column in a data frame, we first import the tidyverse package and added a new column using tibble.

The output of the new column in a data frame is:

   name age gender total_marks
1   Tom  20      M          72
2   Kim  21      F          77
3   Sam  19      M          65
4 Julie  20      F          80
5 Emily  21      F          85
6 Chris  22      M          87

Add New Column to Data Frame after the Column

Using the add_column function() in the tibble package, it can add a new column to the existing data frame at a specific position.

Let’s practice with the above student_info data frame to append a new column after a specific column in the existing data frame.

# Append new column to data frame after specific column
student_data <- student_info %>% add_column(total_marks = marks,.after = "age")
# Print the data frame
student_data 

In the above R code, using the add_column function in R, it added the total_marks column to an existing data frame after the age column.

The output of the above R code is:

   name age total_marks gender
1   Tom  20          72      M
2   Kim  21          77      F
3   Sam  19          65      M
4 Julie  20          80      F
5 Emily  21          85      F
6 Chris  22          87      M

Conclusion

I hope the above article about how to add a new column to the data frame in R is helpful to you.

Using the $ operator or square bracket, you can append a new column to a data frame.

You can use the add_column function of the tibble package in tidyverse to add a new column to an existing data frame. It can also add a new column to a specific position in an existing data frame.

Leave a Comment