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.
1 2 3 4 |
movies <- c('Zootopia','The Jungle Book','Mad Max: Fury Road') years <- c(2016, 2016, 2015) ratings <- c('98%', '95%', '97%') df <- data.frame(movies, years, ratings) |
Let’s see the data frame:
1 2 3 4 5 |
> df movies years ratings 1 Zootopia 2016 98% 2 The Jungle Book 2016 95% 3 Mad Max: Fury Road 2015 97% |
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:
1 2 |
> reviews [1] "220" "260" "290" |
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:
1 |
df['reviews'] <- reviews |
Result:
1 2 3 4 5 |
> df movies years ratings reviews 1 Zootopia 2016 98% 220 2 The Jungle Book 2016 95% 260 3 Mad Max: Fury Road 2015 97% 290 |
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:
1 |
DataFrame$new_column_name <- Column_To_Add |
For example,
1 |
df$reviews <- reviews |
Result:
1 2 3 4 5 |
> df movies years ratings reviews 1 Zootopia 2016 98% 220 2 The Jungle Book 2016 95% 260 3 Mad Max: Fury Road 2015 97% 290 |
2.3. Using the cbind() function
The cbind function can be used to add a column to a data frame. For example:
1 |
df <- cbind(df, reviews) |
1 2 3 4 5 |
> df movies years ratings reviews 1 Zootopia 2016 98% 220 2 The Jungle Book 2016 95% 260 3 Mad Max: Fury Road 2015 97% 290 |
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.
1 |
df[['reviews']] <- reviews |
1 2 3 4 5 |
> df movies years ratings reviews 1 Zootopia 2016 98% 220 2 The Jungle Book 2016 95% 260 3 Mad Max: Fury Road 2015 97% 290 |
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:
1 |
DataFrame[, 'new_column_name'] <- Column_To_Add |
For example,
1 |
df[, 'reviews'] <- reviews |
1 2 3 4 5 |
> df movies years ratings reviews 1 Zootopia 2016 98% 220 2 The Jungle Book 2016 95% 260 3 Mad Max: Fury Road 2015 97% 290 |
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 – Rename Column of Data Frame
R Data Frame and basic functions
R – Rename Column of Data Frame