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 –
It returns a new dataframe with the passed column appended to the existing dataframe columns.
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)
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.
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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)
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)
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.