This tutorial is going to cover how to add a column to a data frame, which is often required when we manipulate our data using the data frame in R.

1. Preparation

Let’s say that we have a data frame about movies as the following and we’re going to use it to practice on how to add new columns to an existing data frame in R.

Let’s see the data frame:

2. Add A Column To A Data Frame In R

The above data frame has 3 columns movies, years, ratting and now let’s assume we have a reviews column which represents the numbers of reviews for each movie, and we want to add that column into the existing df data frame.

At first, let’s see the column:

There are several ways to add columns to an existing data frame, and we will try one by one as follows:

2.1. DataFrame[‘new_column_name’] <- Column_To_Add

For example, to add the reviews vector as a new column to the data frame df:

Result:

With this way, it feels like we have just assigned a column to an existing data frame.

2.2. DataFrame$new_column_name <- Column_To_Add

If we’re familiar with using the Dollar sign ($) to access to columns of a data frame, we can use it to add a new column to a data frame as follow:

For example,

Result:

2.3. Using the cbind() function

The cbind function can be used to add a column to a data frame. For example:

2.4. DataFrame[[‘new_column_name’]] <- Column_To_Add

In similar to 2.2., we can use [[ operator to access and add columns to a data frame.

2.5. DataFrame[, ‘new_column_name’] <- Column_To_Add

Another way for us to add a column to an existing data frame in R is to use the syntax:

For example,

3. Conclusion

The tutorial has illustrated us different ways to add a column to a data frame in R. You can select any one that is easy and familiar with you.

4. References

R – How To Order A Data Frame

R – Rename Column of Data Frame

R Data Frame and basic functions

R – Rename Column of Data Frame

0 0 vote
Article Rating