add column to an existing dataframe in R

How to Add a Column to a Dataframe in R?

The R programming language comes with a number of helpful functions to work with and manipulate data in dataframes. In this tutorial, we will look at how to add a column to an existing dataframe in R with the help of some examples.

How do I add a column to a dataframe in R?

You can use the cbind() function to add an additional column to an existing dataframe in R. Pass the existing dataframe and the new column data as arguments to the function.

The following is the syntax –

cbind(existing_dataframe, new_column_name=new_column_data)

It returns a new dataframe with the passed column appended to the existing dataframe columns.

Examples

Let’s now look at some examples of using the above syntax in action.

First, we will create our existing dataframe.

# create a dataframe
students_df = data.frame(
  "Name"= c("Ben", "Quinton", "Virat", "Smriti", "Jos"),
  "Age"= c(23, 27, 24, 21, 26)
)
# display the dataframe
print(students_df)

Output:

     Name Age
1     Ben  23
2 Quinton  27
3   Virat  24
4  Smriti  21
5     Jos  26

The above dataframe contains the name and age information of some students in a university in the columns “Name” and “Age” respectively.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

Add a single column to an existing dataframe in R

Let’s say you want to add one more column to the above dataframe—for example, a column with information on the native country of the students.

Here, we will use the cbind() function add an additional column to the dataframe created above.

# add Height column to students_df
students_df = cbind(students_df, "Country"=c("UK", "South Africa", "India", "India", "UK"))
# display the dataframe
print(students_df)

Output:

     Name Age      Country
1     Ben  23           UK
2 Quinton  27 South Africa
3   Virat  24        India
4  Smriti  21        India
5     Jos  26           UK

You can see that the dataframe students_df now has an additional column “Country” with information on the native country of the respective students.

Add multiple columns to an existing dataframe

You can similarly use the cbind() function to add more than one column to an existing dataframe. For example, let’s add columns with the height (in cm) and weight (in kg) information of the students.

# add multile columns to students_df
students_df = cbind(students_df, "Height"=c(184, 171, 170, 168, 176), "Weight"=c(88, 81, 76, 63, 82))
# display the dataframe
print(students_df)

Output:

     Name Age      Country Height Weight
1     Ben  23           UK    184     88
2 Quinton  27 South Africa    171     81
3   Virat  24        India    170     76
4  Smriti  21        India    168     63
5     Jos  26           UK    176     82

You can see that the resulting dataframe has two new columns “Height” and “Weight”.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Authors

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

  • Gottumukkala Sravan Kumar
Scroll to Top