20.7 C
London
Monday, June 2, 2025
HomeStatistics TutorialRHow to Add a Column to a Data Frame in R (With...

How to Add a Column to a Data Frame in R (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

There are three common ways to add a new column to a data frame in R:

1. Use the $ Operator

df$new 

2. Use Brackets

df['new'] 

3. Use Cbind

df_new cbind(df, new)

This tutorial provides examples of how to use each of these methods in practice using the following data frame:

#create data frame
df frame(a = c('A', 'B', 'C', 'D', 'E'),
                 b = c(45, 56, 54, 57, 59))

#view data frame
df

  a  b
1 A 45
2 B 56
3 C 54
4 D 57
5 E 59

Example 1: Use the $ Operator

The following code shows how to add a column to a data frame by using the $ operator:

#define new column to add
new 
#add column called 'new'
df$new 
#view new data frame
df 

  a  b new
1 A 45   3
2 B 56   3
3 C 54   6
4 D 57   7
5 E 59   8

Example 2: Use Brackets

The following code shows how to add a column to a data frame by using brackets:

#define new column to add
new 
#add column called 'new'
df['new'] 
#view new data frame
df 

  a  b new
1 A 45   3
2 B 56   3
3 C 54   6
4 D 57   7
5 E 59   8

Example 3: Use Cbind

The following code shows how to add a column to a data frame by using the cbind function, which is short for column-bind:

#define new column to add
new 

#add column called 'new'
df_new cbind(df, new)

#view new data frame
df_new

  a  b new
1 A 45   3
2 B 56   3
3 C 54   6
4 D 57   7
5 E 59   8

You can actually use the cbind function to add multiple new columns at once:

#define new columns to add
new1 
#add columns called 'new1' and 'new2'
df_new cbind(df, new1, new2)

#view new data frame
df_new

  a  b new1 new2
1 A 45    3   13
2 B 56    3   14
3 C 54    6   16
4 D 57    7   17
5 E 59    8   20

Bonus: Set Column Names

After adding one or more columns to a data frame, you can use the colnames() function to specify the column names of the new data frame:

#create data frame
df frame(a = c('A', 'B', 'C', 'D', 'E'),
                 b = c(45, 56, 54, 57, 59),
                 new1 = c(3, 3, 6, 7, 8),
                 new2 = c(13, 14, 16, 17, 20))

#view data frame
df

  a  b new1 new2
1 A 45    3   13
2 B 56    3   14
3 C 54    6   16
4 D 57    7   17
5 E 59    8   20

#specify column names
colnames(df) #view data frame
df

  a  b c  d
1 A 45 3 13
2 B 56 3 14
3 C 54 6 16
4 D 57 7 17
5 E 59 8 20

You can find more R tutorials here.

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories