How to Add Column to R Data Frame

R data frame is a list of vectors that are of equal length. The data frame accepts different data types (numeric, character, factor, etc.). Performing crud operations on the R data frame is a frequent operation, and it can be done in various ways.

Add column to R data frame.

Data Frame in R has a tabular data structure in which a row contains a set of values, and each column holds the values of one variable. After creating a data frame, you often modify it by adding or removing columns.

To add a column to the R data frame, use one of the following approaches.

  1. Add column using $ operator
  2. Add column with square ([ ]) brackets
  3. Add column with cbind() function

Before adding a new column, first, we need a data frame to work with. So let’s create a data frame.

mando <- data.frame(
 character_id = c(1:5),
 character_name = c("Mandalorian", "Grogu", "Ahsoka", "Bo Katan", "Moff Gideon"),
 character_age = c(30, 50, 29, 28, 50),
 stringsAsFactors = FALSE
)

# Print the data frame.
print(mando)

Output

Add Column to R Data Frame

You can see that our data frame has 3 columns and 5 rows.

To add a new column, you need a column name and its values. In our case, it has five values because each column has five values. The stringsAsFactors parameter converts a string to a factor by default. That is why we pass FALSE.

Let’s define a vector consists of column values.

rv_new <- c(900, 1000, 600, 400, 800)

The number of rows of our data frame and the length of our vector are equal.

Add column to the data frame using $ operator

To add a new column in the R data frame, use the $ operator. Assign vector to the new column, which ultimately adds a new column with five rows in the data frame.

mando$character_bounty <- rv_new

In this code, mando is our data frame, character_bounty is our new column name, added in the data frame, and rv_new is a vector that is a column value.

So our final code looks like below.

mando <- data.frame(
 character_id = c(1:5),
 character_name = c("Mandalorian", "Grogu", "Ahsoka", "Bo Katan", "Moff Gideon"),
 character_age = c(30, 50, 29, 28, 50),
 stringsAsFactors = FALSE
)
# Print the data frame.
mando
rv_new <- c(900, 1000, 600, 400, 800)

# add a new column
mando$character_bounty <- rv_new
cat("After adding a new column", "\n")
mando

Output

Add column to data frame using $ operator

You can see from the output that the new column character_bounty is added with five values.

Add Column with Square Brackets

Square brackets are another approach through which you can add a new column in the R data frame.

mando["character_bounty"] <- rv_new

Instead of using the $ operator to attach a new column to the data frame, use the square brackets to attach a new column and assign the values to that column.

mando <- data.frame(
 character_id = c(1:5),
 character_name = c("Mandalorian", "Grogu", "Ahsoka", "Bo Katan", "Moff Gideon"),
 character_age = c(30, 50, 29, 28, 50),
 stringsAsFactors = FALSE
)
# Print the data frame.
mando
rv_new <- c(900, 1000, 600, 400, 800)

# add a new column using [ ] brackets
mando["character_bounty"] <- rv_new
cat("After adding a new column", "\n")
mando

Output

Add column to data frame using $ operator

Add column with cbind() function in R data frame

The cbind() function in R takes a sequence of vector, matrix, or data frame arguments and combines them by columns or rows.

That is why the cbind() function can add a column to a data frame as follows.

mando <- cbind(mando, character_bounty = rv_new)

The cbind() function takes the data frame, the new column name, and its values. In our case, the new column name is character_bounty, and its value is vector rv_new.

mando <- data.frame(
 character_id = c(1:5),
 character_name = c("Mandalorian", "Grogu", "Ahsoka", "Bo Katan", "Moff Gideon"),
 character_age = c(30, 50, 29, 28, 50),
 stringsAsFactors = FALSE
)
# Print the data frame.
mando
rv_new <- c(900, 1000, 600, 400, 800)

# add a new column using cbind() function
mando <- cbind(mando, character_bounty = rv_new)
cat("After adding a new column", "\n")
mando

It will give you the same output as above. As you can see, we have seen three techniques to add a data frame column in r.

Creating a data frame, reading a data frame, modifying a data frame, and deleting a data frame is a common operation every data scientist performs, and R language has built-in support to do that.

See also

How to add vectors in R

Sort in R

Length in R

Leave a Comment